Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle two different submit operation from same form in a spring controller

Tags:

spring-mvc

jsp

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?

like image 317
sara Avatar asked Jul 12 '11 00:07

sara


1 Answers

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.

like image 63
skaffman Avatar answered Oct 16 '22 16:10

skaffman