Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Multiple submit button to a Form

I am trying to have 2 submit buttons post to a form, with each button action mapped to different controllers. Here are my mappings

@RequestMapping(value="/save", method=RequestMethod.POST, params="save")
@RequestMapping(value="/save", method=RequestMethod.POST, params="renew")

And my submit buttons look like these -

<input type="submit" name="save" class="button" value="Save" />
<input type="submit" name="renew" class="button" value="Renew" />

As you can see from my mapping, I am relying on the use of params to differentiate what button was clicked on. The problem is that it works 90% of the time but sometimes I get the exception below -

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8090/myapp/save': {public java.lang.String com.myapp.SaveController.save(MyEntity,javax.servlet.http.HttpSession), public java.lang.String com.myapp.SaveController.saveAndRenew(MyEntity,javax.servlet.http.HttpSession)}
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:248)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:194)

Strangely, when this happens and I re-submit the page, everything works fine afterwards. Is there a better way to achieve what I'm trying to do ?

Thanks!

like image 492
user558122 Avatar asked Jan 21 '12 16:01

user558122


People also ask

Can you have multiple submit buttons in a form MVC?

One Form can do a POST submission to one Action method in Controller and hence in order to use multiple Submit buttons inside one single Form, a Switch case has to be implemented inside the Action method.

How do I apply multiple submit buttons in one form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can a form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Which function is used to handle multiple submit buttons?

Multiple buttons with different names To display a data entry textboxes EditorForModel() helper is used. You can very well use helpers such as TextBoxFor() and LabelFor() if you so wish. There are two submit buttons - one with name attribute set to save and the other with name of cancel.


4 Answers

if the form has these buttons specified:

input type="submit" class="button" name="save" value="Save"
input type="submit" class="button" name="delete" value="Delete"
input type="submit" class="button" name="cancel" value="Cancel"

you may direct to different url request according to button pressed with one controller.

for cancel button,

@RequestMapping(params = "cancel", method = RequestMethod.POST)
public String cancelUpdateUser(HttpServletRequest request) {
    return "redirect:/users.html";
}

what request mapping does is to scan post request if it contains params name = cancel.

for save button,

@RequestMapping(params = "save", method = RequestMethod.POST)
public String saveUser(HttpServletRequest request, @ModelAttribute User user, BindingResult result, SessionStatus status) {
    // validate your result
    // if no errors, save it and redirect to successView.
}
like image 185
xyzlast Avatar answered Oct 19 '22 11:10

xyzlast


Why not:

<input type="submit" name="action" value="save" />

and then:

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam String action){

    if( action.equals("save") ){
       //handle save
    }
    else if( action.equals("renew") ){
       //handle renew
    }

} 
like image 43
Dan Avatar answered Oct 19 '22 12:10

Dan


If You have more controller methods with the same @RequestMapping that differs only in params attribute, You have to explicitly write:

  • which parameter is supposed to be present in the request, e.g. params="save"
  • which parameter is NOT supposed to be present in the request, e.g. params="!save"

In Your case:

@RequestMapping(value="/save", method=RequestMethod.POST, params={"save", "!renew"})
@RequestMapping(value="/save", method=RequestMethod.POST, params={"renew", "!save"})

This should fix error Ambiguous handler methods mapped for HTTP path ...

See Spring Web API 4.0.x - RequestMapping#params

like image 10
lu_ko Avatar answered Oct 19 '22 13:10

lu_ko


Just create one controller with a method similar to this

@RequestMapping(value="/save", method=RequestMethod.POST)
public String handlePost(@RequestParam(required=false , value = "save") String saveFlag , @RequestParam(required=false , value = "renew") String renewFlag){

if(saveFlag != null{
   //handle save
}
else if(renewFlag !=null{
   //handle renew
}

} 
like image 7
J. Moreno Avatar answered Oct 19 '22 12:10

J. Moreno