struts一个action处理多个方法

在前面的介绍中,我们了解了struts的action是实现execute()方法来完成业务逻辑的,可是,在实际开发中,让一个业务逻辑对应一个Acton类是不现实的,通常我们都是在一个action中定义多个方法的。

       下面了解一下strtus1和struts2在一个action中处理多个方法的实现。

struts1:

           struts1中一个action处理多个方法,通过让action继承DispachAction来实现

           1. action继承DispachAction,并实现多个方法的业务逻

 

  1. public class LogonAction extends DispatchAction {  
  2.       
  3.     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  4.         //业务逻辑  
  5.         return mapping.findForward("success");  
  6.     }         
  7.     public ActionForward logon(ActionMapping mapping, ActionForm form,  
  8.             HttpServletRequest request, HttpServletResponse response)throws Exception {  
  9.         return mapping.findForward("success");  
  10.     }  
  11.               
  12.     public ActionForward register(ActionMapping mapping, ActionForm form,  
  13.             HttpServletRequest request, HttpServletResponse response)throws Exception {  
  14.         return mapping.findForward("success");  
  15.     }  
  16.               


          2. strtus-config.xml配置文件中,每个action标签后面,加上parameter=""这个属性

[html] view plain copy

 

  1. <action path="/user" type="com.tgb.struts1.LogonAction" name="logonForm"  parameter="method">  
  2.     <forward name="success" path="/success.jsp"></forward>  
  3.     <forward name="error" path="/error.jsp"></forward>  
  4. </action>  

           3. 页面提交时确定执行action中的哪个方法,如果不指定,则执行action中的unspecified()方法.

[html] view plain copy

 

  1. <form action="user" mehtod="post">  
  2.     <input type="hidden" name="method" value="logon">  



struts2:

       action中定义多个方法:

[java] view plain copy

 

  1. public String logon(){  
  2. eturn"success";  
  3. }  
  4.   
  5. publicString register(){  
  6.     return"success";  
  7.   
  8. }  

     有三种方式可以将页面提交和action的方法对应。

     (一)动态方法调用,配置文件不变,一个action类对应配置文件中一个action标签,表单提交的action不直接等于某个action的name,而是以action的name!action的方法名来提交。

    1. 配置文件

[html] view plain copy

 

  1. <action name="user" class="com.tgb.struts2.action.LogonAction" >  
  2.     <result name="success">/success.jsp</result>  
  3.     <result name="error">/error.jsp</result>  
  4. </action>  

      2. 表单提交

[html] view plain copy

 

  1. 登录:<form id="form" action="user!logon" method="post">            
  2. 注册:<form id="form" action="user!register" method="post">  

 

    (二)修改配置文件,将同一个action中的每个方法都用一个action标签映射
       1. 配置文件

[html] view plain copy

 

  1. <action name="logon" class="com.tgb.struts2.action.LogonAction" >  
  2.     <result name="success">/success.jsp</result>  
  3.     <result name="error">/error.jsp</result>  
  4. </action>  
  5. <action name="register" class="com.tgb.struts2.action.LogonAction" >  
  6.     <result name="success">/success.jsp</result>  
  7.     <result name="error">/error.jsp</result>  
  8. </action>  

     2. 表单提交

[html] view plain copy

 

  1. 登录:<form id="form" action="logon" method="post">               
  2. 注册:<form id="form" action="register" method="post">  

   (三)使用通配符映射方式

    1. 配置文件

[html] view plain copy

 

  1. <action name="*User_*" class="com.tgb.struts2.action.LogonAction" method="{1}" >            
  2. <result name="success">/{2}.jsp</result>  
  3. <result name="error">/error.jsp</result>  
  4. lt;/action>  

     2. 表单提交

[html] view plain copy

 

  1. <form id="form" action="logonUser_index" method="post"><!-- 执行action中的logon方法,执行成功后返回index首页面 -->  
  2. nbsp;       <form id="form" action="registerUser_logon" method="post"><!--执行action中的register方法,执行成功后返回logon登录页面  -->  

        这三种方式中,使用!会暴露所使用的框架,第二种方式会使配置文件变得复杂冗余,第三种方式中和了前两种方式的缺点,建议使用通配符映射方式。

转载于:https://my.oschina.net/chenliyong/blog/1036500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值