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

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

javascript - Restarting Supervisor and effect on FlaskSocketIO -

php - Mongodb connectivity error -