如何在Spring Controller中处理来自同一表单的两个不同的提交操作 [英] How to handle two different submit operation from same form in a spring controller

查看:53
本文介绍了如何在Spring Controller中处理来自同一表单的两个不同的提交操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP的添加,删除中有两个提交按钮.我真的不知道如何区分控制器端的操作.

I have two submit buttons in my JSP add,remove. I don't really know how to differentiate the operations in the controller side.

<form:form modelAttribute="emp" action="/empl" method="POST"> 
    <input type="submit" name="operation" value="Remove"/>
    <input type="submit" name="operation" value="Add" />
</form:form>


@RequestMapping(value = "/empl", method = RequestMethod.POST)
public String getD(@Valid Em form, BindingResult result, Model model) {//code}

这两个按钮的onclick分别正确地将值与操作添加"或操作删除"一起发布,然后添加作品,因为这是被调用的默认方法.现在如何捕获操作参数并区分操作并使用它?

Onclick of the either button posts values correctly along with "operation Add" or "operation Remove" respectively and then add works because that is the default method being called. Now how do i catch the operation parameter and differentiate the operation and use it?

推荐答案

浏览器将提供一个请求参数,其中包含按下的提交按钮的名称.然后,您可以使用它们进行过滤:

The browser will provide a request parameter containing the name of the submit button that was pressed. You can then use those to filter:

@RequestMapping(value="/empl", method=RequestMethod.POST, params="operation=Remove")
public String remove(@Valid Em form, BindingResult result, Model model)

@RequestMapping(value="/empl", method=RequestMethod.POST, params="operation=Add")
public String add(@Valid Em form, BindingResult result, Model model)

然后每个请求都可以根据需要调用共享逻辑.

Each of these can then call shared logic as required.

这篇关于如何在Spring Controller中处理来自同一表单的两个不同的提交操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆