我有一个带有一个表单和两个按钮的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>
Run Code Online (Sandbox Code Playgroud)
和控制器:
@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;
}
Run Code Online (Sandbox Code Playgroud)
这项工作很好,但如果我有更多按钮,我必须添加更多if语句来检查action字符串.还有另一种方法可以为表单中的每个按钮创建一个动作吗?