java 如何检测在单个 Action 类中的多个提交按钮场景中单击的提交按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13343954/
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
How to detect submit button clicked in multiple submit buttons scenario in single Action class?
提问by null
I have a form in a jsp. There are two submit buttons: "Search" and "Add New" button.
我在jsp中有一个表单。有两个提交按钮:“搜索”和“添加新”按钮。
<s:form name="searchForm" action="employeeAction" method="post">
<s:textfield name="id" label="Employee ID"/>
<s:textfield name="name" label="Employee Name"/>
<s:submit value="Search"/>
<s:submit value="Add New"/>
</s:form>
In struts.xml
在 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package>
<package name="example" namespace="/example" extends="default">
<action name="employeeAction" class="example.EmployeeAction">
<result name="search">/example/search.jsp</result>
<result name="add" type="redirect">/example/add.jsp</result>
</action>
</package>
</struts>
In Struts Action class, we know that there is only one method that processing http request, that is execute()
method.
在Struts Action类中,我们知道处理http请求的方法只有一种,就是execute()
方法。
In my expected case, when I clicked Searchbutton, it will perform searching data and render data to /example/search.jsp
, when I clicked Add Newbutton, it will perform redirecting page to /example/add.jsp
. However, both buttons when clicked will go into execute() method. So I need to know how to detect which button clicked in the execute()
method.
在我预期的情况下,当我单击“搜索”按钮时,它将执行搜索数据并将数据呈现到/example/search.jsp
,当我单击“添加新”按钮时,它将执行重定向页面到/example/add.jsp
。但是,两个按钮在被点击时都会进入 execute() 方法。所以我需要知道如何检测execute()
方法中点击了哪个按钮。
The scenario looks like this
场景是这样的
public class EmployeeAction extends ActionSupport {
public String execute() throws Exception {
//PSEUDOCODE
//IF (submitButton is searchButton)
// return doSearch();
//ELSE IF (submitButton is addNewButton)
// return doAddNew();
return SUCCESS;
}
public String doSearch() throws Exception {
//perform search logic here
return "search";
}
public String doAddNew() throws Exception {
return "add";
}
}
回答by Aleksandr M
You can define two actions in struts.xml
file and use action
attribute of <s:submit>
tag in order to submit to different actions http://struts.apache.org/docs/submit.html.
您可以在struts.xml
文件中定义两个动作并使用标签的action
属性,<s:submit>
以便提交到不同的动作http://struts.apache.org/docs/submit.html。
In JSP:
在 JSP 中:
<s:submit value="Search" action="searchEmployeeAction"/>
<s:submit value="Add New" action="addEmployeeAction"/>
In struts.xml:
在 struts.xml 中:
<action name="addEmployeeAction" method="add" class="example.EmployeeAction">
<result>/example/add.jsp</result>
</action>
<action name="searchEmployeeAction" method="search" class="example.EmployeeAction">
<result>/example/search.jsp</result>
</action>
And in your action create two public String
methods add
and search
.
并在您的操作中创建两个public String
方法add
和search
.
Read about Multiple Submit Buttons http://struts.apache.org/docs/multiple-submit-buttons.html.
阅读多个提交按钮http://struts.apache.org/docs/multiple-submit-buttons.html。
Update
更新
Starting from Struts2 version 2.3.15.3 you need to set struts.mapper.action.prefix.enabled
constant to true in order to enable support for action:
prefix.
从 Struts2 版本 2.3.15.3 开始,您需要将struts.mapper.action.prefix.enabled
常量设置为 true 以启用对action:
前缀的支持。
Put that in your struts.xml
file:
把它放在你的struts.xml
文件中:
<constant name="struts.mapper.action.prefix.enabled" value="true" />
回答by Sumit Desai
In your model layer, define a String
property named 'button'. Now, for both your submit buttons, specify name
or property
attribute as 'button'. So, in your execute()
method, in property 'button', you will get the corresponding value.
在您的模型层中,定义一个String
名为“button”的属性。现在,对于您的提交按钮,指定name
或property
属性为“按钮”。因此,在您的execute()
方法中,在属性“button”中,您将获得相应的值。