I have two submit buttons in my JSP add,remove. I don't really know how to differentiate the operations in 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}
Onclick of the either button posts values correctly along with "operation Add" or "operation Remove" respectively and then add works because that is the default method being called. Now how do i catch the operation parameter and differentiate the operation and use it?
The browser will provide a request parameter containing the name of the submit button that was pressed. You can then use those 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 these can then call shared logic as required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With