公司怎么找做网站,华龙网,商城网站开发解决方案,河南响应式建站今天遇见一个form需要两个button提交显示不同Controller的需求
一开始解决思路#xff1a; 我有一个HTML页面的一个窗体和2个按钮的片段#xff1a;
form action# data-th-action{/action/edit} data-th-object${model} method 我有一个HTML页面的一个窗体和2个按钮的片段
form action# data-th-action{/action/edit} data-th-object${model} methodpostbutton typesubmit nameaction valuesavesave/buttonbutton typesubmit nameaction valuecancelcancel/button
/form以及控制器
RequestMapping(value/edit, methodRequestMethod.POST)
public ModelAndView edit(ModelAttribute SomeModel model, RequestParam(valueaction, requiredtrue) String action) {if (action.equals(save)) {// do something here }if (action.equals(cancel)) {// do another thing}return modelAndView;
}后来网上查阅大牛的思路 您可以使用params变量创建具有不同RequestMappings的单独方法。
RequestMapping(value/edit, methodRequestMethod.POST, paramsactionsave)
public ModelAndView save() {}
RequestMapping(value/edit, methodRequestMethod.POST, paramsactioncancel)
public ModelAndView cancel() {}使用thformactionon submit button这是工作在多少你有submit button和这也是usefull给予更多的链接到一个表单与不同的submit button
form action# classform th:action{/publish-post/${post.id}} methodpost
input classsavebtn typesubmit valueSave th:formaction/save-post/${post.id}
input classpublish typesubmit valuePublish Article
/form对于多个提交按钮下面的方法对我很有效。* 注意th取消按钮中的格式操作。*
form action# th:action{/saveProducts} th:object${model} methodpostbutton typesubmit nameaction valuecancel th:formaction{/cancelProducts}CANCEL/buttonbutton typesubmit nameaction valuesaveSAVE/button
/form对于控制器
RequestMapping(value/saveProducts, method RequestMethod.POST)public String save(ModelAttribute SomeModel model) {*//Do something*}RequestMapping(value/cancelProducts, methodRequestMethod.POST)public String cancel(ModelAttribute SomeModel model) {*//Do something*}