35

I have a fragment of HTML page with one form and 2 button:

<form action="#" data-th-action="@{/action/edit}" data-th-object="${model}" method="post">
    <button type="submit" name="action" value="save">save</button>
    <button type="submit" name="action" value="cancel">cancel</button>
</form>

And the controller:

@RequestMapping(value="/edit", method=RequestMethod.POST)
public ModelAndView edit(@ModelAttribute SomeModel model, 
        @RequestParam(value="action", required=true) String action) {

    if (action.equals("save")) {
        // do something here     
    }

    if (action.equals("cancel")) {
       // do another thing
    }
    return modelAndView;
}

This work good, but if I have more button, I must add more if statement to check the action string. Is there another way that I can create one action for each button in the form?

2
  • The Form action url is "@{/action/edit}" but in your controller "/edit",can you please tell me how you are linking your html & controller. Aug 9, 2018 at 6:59
  • Pranesh Sahu, maybe a prefix @RequestMapping(value="action") on a controller class?
    – Zon
    Jan 7, 2020 at 17:39

6 Answers 6

49

You can create separate methods with different @RequestMappings using the params variable.

@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=save")
public ModelAndView save() {}


@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=cancel")
public ModelAndView cancel() {}
0
7

this works in my problem. use th:formaction on submit button this is work on how many you have submit button and this is also usefull for give more links to one form with different submit button

<form action="#"  class="form" th:action="@{'/publish-post/'+${post.id}}" method="post">
<input class="savebtn" type="submit" value="Save" th:formaction="'/save-post/'+${post.id}">
<input class="publish" type="submit" value="Publish Article">
</form>
1
  • Very nice! Much cleaner than accepted answer, as it allows for well defined methods in Controller.
    – Mikezx6r
    Aug 15, 2022 at 2:23
6

Instead of an if-case you could have a switch case, should you not want to take in every option as a new request mapping.

@RequestMapping(value="/edit", method=RequestMethod.POST)
public ModelAndView edit(@ModelAttribute SomeModel model, 
        @RequestParam(value="action", required=true) String action) {
    switch(action) {
        case "save":
            // do stuff
            break;
        case "cancel":
            // do stuff
            break;
        case "newthing":
            // do stuff
            break;
        default:
            // do stuff
            break;
    }
}
1
  • Quite late to notice, but break; in default is heavily redundant (as the switch statement exits anyway).
    – Egor Hans
    Mar 22, 2021 at 9:24
2

For multiple submit buttons, the below worked for me. Notice the: th:formaction in the cancel button.

<form action="#" th:action="@{/saveProducts}" th:object="${model}" method="post">
  <button type="submit" name="action" value="cancel" th:formaction="@{/cancelProducts}">CANCEL</button>
  <button type="submit" name="action" value="save">SAVE</button>
</form>

For the controller:

   @RequestMapping(value="/saveProducts", method= RequestMethod.POST)
    public String save(@ModelAttribute SomeModel model) {
      *//Do something*
    }

    @RequestMapping(value="/cancelProducts", method=RequestMethod.POST)
    public String cancel(@ModelAttribute SomeModel model) {
      *//Do something*

    }
0

HTML Code:

<div class="inner">
    <a href="#" th:onclick="|deleteContact(${c.cId})|" class="btn btn-danger btn-small">Delete</a>
<div class="inner">
    <form th:action="@{'/update-contact/'+${c.cId}}">
        <button type="submit" class="btn btn-primary btn-small">Update</button>
    </form>
</div>
</div>

CSS Code:

.inner {
    display: inline-block;
}

Here is output:

-3

You can know which submit button has been clicked and then act upon the button Here is the code

String btnName = request.getParameter("action");

if(btnName.equals("save"))
    // you code....
else if(btnName.equals("cancel"))
    // you code....
1
  • To clarify the downvotes: Not only is this entirely different from the point of the question (looking for ways to avoid the if construct in this situation), but also, using request.getParameter(...) is way worse than wiring it directly to a method parameter using @RequestParam (as done in the question's code).
    – Egor Hans
    Mar 22, 2021 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.