I have a form in jsp. There are two submit buttons: "Search" and "Add New" button. I had set each button with their own method attribute.
我在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" method="doSearch"/>
<s:submit value="Add New" method="doAddNew"/>
</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">/example/add.jsp</result>
</action>
</package>
</struts>
In EmployeeAction class
在EmployeeAction类
public class EmployeeAction extends ActionSupport {
private static final Logger logger = Logger.getLogger(EmployeeAction.class);
@Override
public String execute() throws Exception {
logger.info("Calling execute!");
return SUCCESS;
}
public String doSearch() throws Exception {
logger.info("Calling doSearch!");
return "search";
}
public String doAddNew() throws Exception {
logger.info("Calling doAddNew!");
return "add";
}
}
The problem is when I clicked "Search" or "Add New" button, the method doSearch() or doAddNew() was never called, instead it called method execute(). What is wrong with my code above?
问题是,当我点击“搜索”或“添加新的”按钮时,方法doSearch()或doAddNew()从未被调用,而是称为方法execute()。上面的代码有什么问题?
I'm using struts v2.3.
我使用struts v2.3。
1 个解决方案
#1
6
Set
集
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
to
来
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Another way is to define multiple mappings for the same Action, like
另一种方法是为相同的操作定义多个映射。
in JSP:
JSP:
<s:submit value="Search" action="employeeSearchAction" />
<s:submit value="Add New" action="employeeAddNewAction"/>
in Struts.xml
在Struts.xml
<action name="employeeSearchAction" class="example.EmployeeAction" method="doSearch">
<result>/example/search.jsp</result>
</action>
<action name="employeeAddNewAction" class="example.EmployeeAction" method="doAddNew">
<result>/example/add.jsp</result>
</action>
A third way is to use Wildcard Mappings.
第三种方法是使用通配符映射。
P.S: If you go for the second one, I'll suggest, as a best practice, to use one Action for every logical action you have to perform...
P。S:如果你选择第二种,我建议你最好的做法是,为每一个你必须要做的逻辑动作使用一个动作……
If you have common data loaded / managed by both your actions, "search" and "addNew", then you can define a employeeBaseAction, extended by both employeeSearchAction and employeeAddNewAction.
如果您的操作、“搜索”和“addNew”都有共同的数据加载/管理,那么您可以定义employeeBaseAction,它由employeeSearchAction和employeeAddNewAction扩展。
EDIT
It's 2014 now, and DMI usage is unanimously discouraged (today more than ever), other than pretty useless, so I strongly suggest you to use solution n.2.
现在已经是2014年了,DMI的使用是一致的(今天比以往任何时候都要多),除了无用,所以我强烈建议您使用解决方案。
#1
6
Set
集
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
to
来
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Another way is to define multiple mappings for the same Action, like
另一种方法是为相同的操作定义多个映射。
in JSP:
JSP:
<s:submit value="Search" action="employeeSearchAction" />
<s:submit value="Add New" action="employeeAddNewAction"/>
in Struts.xml
在Struts.xml
<action name="employeeSearchAction" class="example.EmployeeAction" method="doSearch">
<result>/example/search.jsp</result>
</action>
<action name="employeeAddNewAction" class="example.EmployeeAction" method="doAddNew">
<result>/example/add.jsp</result>
</action>
A third way is to use Wildcard Mappings.
第三种方法是使用通配符映射。
P.S: If you go for the second one, I'll suggest, as a best practice, to use one Action for every logical action you have to perform...
P。S:如果你选择第二种,我建议你最好的做法是,为每一个你必须要做的逻辑动作使用一个动作……
If you have common data loaded / managed by both your actions, "search" and "addNew", then you can define a employeeBaseAction, extended by both employeeSearchAction and employeeAddNewAction.
如果您的操作、“搜索”和“addNew”都有共同的数据加载/管理,那么您可以定义employeeBaseAction,它由employeeSearchAction和employeeAddNewAction扩展。
EDIT
It's 2014 now, and DMI usage is unanimously discouraged (today more than ever), other than pretty useless, so I strongly suggest you to use solution n.2.
现在已经是2014年了,DMI的使用是一致的(今天比以往任何时候都要多),除了无用,所以我强烈建议您使用解决方案。