在前面的介绍中,我们了解了struts的action是实现execute()方法来完成业务逻辑的,可是,在实际开发中,让一个业务逻辑对应一个Acton类是不现实的,通常我们都是在一个action中定义多个方法的。
下面了解一下strtus1和struts2在一个action中处理多个方法的实现。
struts1:
struts1中一个action处理多个方法,通过让action继承DispachAction来实现
1. action继承DispachAction,并实现多个方法的业务逻
- public class LogonAction extends DispatchAction {
- public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- //业务逻辑
- return mapping.findForward("success");
- }
- public ActionForward logon(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)throws Exception {
- return mapping.findForward("success");
- }
- public ActionForward register(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)throws Exception {
- return mapping.findForward("success");
- }
2. strtus-config.xml配置文件中,每个action标签后面,加上parameter=""这个属性
[html] view plain copy
- <action path="/user" type="com.tgb.struts1.LogonAction" name="logonForm" parameter="method">
- <forward name="success" path="/success.jsp"></forward>
- <forward name="error" path="/error.jsp"></forward>
- </action>
3. 页面提交时确定执行action中的哪个方法,如果不指定,则执行action中的unspecified()方法.
[html] view plain copy
- <form action="user" mehtod="post">
- <input type="hidden" name="method" value="logon">
struts2:
action中定义多个方法:
[java] view plain copy
- public String logon(){
- eturn"success";
- }
- publicString register(){
- return"success";
- }
有三种方式可以将页面提交和action的方法对应。
(一)动态方法调用,配置文件不变,一个action类对应配置文件中一个action标签,表单提交的action不直接等于某个action的name,而是以action的name!action的方法名来提交。
1. 配置文件
[html] view plain copy
- <action name="user" class="com.tgb.struts2.action.LogonAction" >
- <result name="success">/success.jsp</result>
- <result name="error">/error.jsp</result>
- </action>
2. 表单提交
[html] view plain copy
- 登录:<form id="form" action="user!logon" method="post">
- 注册:<form id="form" action="user!register" method="post">
(二)修改配置文件,将同一个action中的每个方法都用一个action标签映射
1. 配置文件
[html] view plain copy
- <action name="logon" class="com.tgb.struts2.action.LogonAction" >
- <result name="success">/success.jsp</result>
- <result name="error">/error.jsp</result>
- </action>
- <action name="register" class="com.tgb.struts2.action.LogonAction" >
- <result name="success">/success.jsp</result>
- <result name="error">/error.jsp</result>
- </action>
2. 表单提交
[html] view plain copy
- 登录:<form id="form" action="logon" method="post">
- 注册:<form id="form" action="register" method="post">
(三)使用通配符映射方式
1. 配置文件
[html] view plain copy
- <action name="*User_*" class="com.tgb.struts2.action.LogonAction" method="{1}" >
- <result name="success">/{2}.jsp</result>
- <result name="error">/error.jsp</result>
- lt;/action>
2. 表单提交
[html] view plain copy
- <form id="form" action="logonUser_index" method="post"><!-- 执行action中的logon方法,执行成功后返回index首页面 -->
- nbsp; <form id="form" action="registerUser_logon" method="post"><!--执行action中的register方法,执行成功后返回logon登录页面 -->
这三种方式中,使用!会暴露所使用的框架,第二种方式会使配置文件变得复杂冗余,第三种方式中和了前两种方式的缺点,建议使用通配符映射方式。