0

I have a form with 2 submit type buttons(Yes/ No), i would like to handle this form with single @RequestMapping in my controller class. I certainly wish to handle multiple submit in single request mapping method only.

My first question is this possible. Can multiple submit buttons be handled with single request mapping of form action in the controller class ?

If yes, then below is the code I have written. Please suggest if this a correct way of implementing it or if it needs to be updated.

Currently, my code looks like this:

Form.jsp:

<form:form action="doAction">
    <input type="submit" name="buttonClick" class="button" value="yes, do Someting" />
    <input type="submit" name="buttonClick" class="button" value="no, do nothing" />
</form:form>

Controller.java:-

private String buttonClick;
@RequestMapping(value = "/doAction", method = RequestMethod.POST, params="buttonClick") {
    if("yes, do Something".equalsIgnoreCase(buttonClick)) 
        //
    else if("no, do Nothing".equalsIgnoreCase(buttonClick)) 
        //
}

2 Answers 2

1

You can change the form action on button click e.g. to "doAction?buttonClick="+<some value from clicked button>.

Or introduce a hidden input in the form. On click change the input value to reflect clicked button. Then the input is available on server side.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a hidden field and change its value on every button click using jQuery:

 <form:form action="doAction">
    <input type="hidden" name="buttonClick" id="buttonClick" />
    <input type="submit" name="buttonClickYes" class="button" value="yes, do Someting" />
    <input type="submit" name="buttonClickNo" class="button" value="no, do nothing" />
 </form:form>



<javascript>
  $(document).ready(function(){
     $("input[type=submit]").click(function(){
       $("#buttonClick").val($(this).val());
       return true;
     });
  });
</javascript>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.