java - Thymeleaf multiple submit button in one form -


i have fragment of html page 1 form , 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 controller:

@requestmapping(value="/edit", method=requestmethod.post) public modelandview edit(@modelattribute somemodel model,          @requestparam(value="action", required=true) string action) {      if (action.equals("save")) {         // here          }      if (action.equals("cancel")) {        // thing     }     return modelandview; } 

this work good, if have more button, must add more if statement check action string. there way can create one action each button in form?

you can create separate methods different @requestmappings using 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() {} 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -