Java 一种形式的 Thymeleaf 多个提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31401669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf multiple submit button in one form
提问by Liem Do
I have a fragment of HTML page with one form and 2 button:
我有一个带有一个表单和两个按钮的 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>
And the 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;
}
This work good, but if I have more button, I must add more if
statement to check the action
string. Is there another way that I can create one actionfor each buttonin the form?
这工作很好,但如果我有更多按钮,我必须添加更多if
语句来检查action
字符串。有没有另一种方法可以为表单中的每个按钮创建一个操作?
采纳答案by Kayaman
You can create separate methods with different @RequestMappings
using the params variable.
您可以@RequestMappings
使用 params 变量创建不同的方法。
@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=save")
public ModelAndView save() {}
@RequestMapping(value="/edit", method=RequestMethod.POST, params="action=cancel")
public ModelAndView cancel() {}
回答by Gemtastic
Instead of an if-case you could have a switch case, should you not want to take in every option as a new request mapping.
如果您不想将每个选项都作为新的请求映射,那么您可以使用 switch case 代替 if-case。
@RequestMapping(value="/edit", method=RequestMethod.POST)
public ModelAndView edit(@ModelAttribute SomeModel model,
@RequestParam(value="action", required=true) String action) {
switch(action) {
case "save":
// do stuff
break;
case "cancel":
// do stuff
break;
case "newthing":
// do stuff
break;
default:
// do stuff
break;
}
}
回答by Backbencher
You can know which submit button has been clicked and then act upon the button Here is the code
您可以知道点击了哪个提交按钮,然后对按钮进行操作这里是代码
String btnName = request.getParameter("action");
if(btnName.equals("save"))
// you code....
else if(btnName.equals("cancel"))
// you code....
回答by Parth Sarkheliya
this works in my problem. use th:formaction on submit button this is work on how many you have submit button and this is also usefull for give more links to one form with different submit button
这适用于我的问题。在提交按钮上使用 th:formaction 这是关于你有多少提交按钮的工作,这对于提供更多链接到一个带有不同提交按钮的表单也很有用
<form action="#" class="form" th:action="@{'/publish-post/'+${post.id}}" method="post">
<input class="savebtn" type="submit" value="Save" th:formaction="'/save-post/'+${post.id}">
<input class="publish" type="submit" value="Publish Article">
</form>