How to handle two different submit operations from the same form in spring controller

I have two submit buttons in my JSP add, remove. I do not know how to distinguish operations on the controller side.

<form:form modelAttribute="emp" action="/empl" method="POST"> 
    <input type="submit" name="operation" value="Remove"/>
    <input type="submit" name="operation" value="Add" />
</form:form>

@RequestMapping(value = "/empl", method = RequestMethod.POST)
public String getD(@Valid Em form, BindingResult result, Model model) {//code}

At any time, click the "+" button, or the "Delete operation", respectively, and then add work, because this is the default method called. Now, how can I grasp the operation parameter and differentiate the operation and use it?

+5
source share
1 answer

The browser will provide a request parameter containing the name of the submit button that was clicked. Then you can use them to filter:

@RequestMapping(value="/empl", method=RequestMethod.POST, params="operation=Remove")
public String remove(@Valid Em form, BindingResult result, Model model)

@RequestMapping(value="/empl", method=RequestMethod.POST, params="operation=Add")
public String add(@Valid Em form, BindingResult result, Model model)

Each of them can then trigger a common logic as necessary.

+6

All Articles