我有一个带有一个表单和两个按钮的 HTML 页面片段:
<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>
还有 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;
}
这项工作很好,但如果我有更多按钮,我必须添加更多 if
语句来检查 action
字符串。还有其他方法可以为表单中的每个按钮创建一个操作吗?
最佳答案
您可以使用 params 变量创建具有不同 @RequestMappings
的单独方法。
@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=save")
public ModelAndView save() {}
@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=cancel")
public ModelAndView cancel() {}
关于java - Thymeleaf 以一种形式提交多个提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401669/