小编Lie*_* Do的帖子

Thymeleaf多个提交按钮在一个表单中

我有一个带有一个表单和两个按钮的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字符串.还有另一种方法可以为表单中的每个按钮创建一个动作吗?

java spring-mvc thymeleaf

20
推荐指数
3
解决办法
2万
查看次数

标签 统计

java ×1

spring-mvc ×1

thymeleaf ×1