构建一个通用的Action 类,请大家谈谈自己的看法和经验

如果为第个Form都建一个Action那是相当麻烦的,

如果像下面这样构建啦一个通用的Action会带来什么好处?

而像平常的做法是几乎是一个Form 和 Action的这种做法又有什么好处?
 
请大家谈谈自己的看法和经验?

下面是源码和相关的解释

请大家先看一下Struts-config.xml 中的一部分内容

xml 代码
  1. <!--第一种 parameter为空并且name为空时的情况 即 form=null 时 直接转发-->  
  2.     <action path="/dgroom/index" type="com.dgroom.struts.BeanAction"  
  3.       validate="false" >  
  4.       <forward name="success" path="/dgroom/index.xsp"/>  
  5.     </action>  
  6. <!--第二种情况 parameter="*"时 意为想调用一个Bean但又不想调用他里面的任何方法时 -->  
  7.    <action path="/dgroom/loginForm" type="com.dgroom.struts.BeanAction"  
  8.       name="accountBean" scope="session" parameter="*"  
  9.       validate="false">  
  10.       <forward name="success" path="/account/loginForm.xsp"/>  
  11.     </action>  
  12. <!--第三种 当  parameter="  NAME  " 有值时 调用 accountBean中的displayAll()方法-->  
  13.     <action path="/dgroom/displayAllAccount" type="com.dgroom.struts.BeanAction"  
  14.       name="accountBean" scope="session" parameter="displayAll"  
  15.       validate="false">  
  16.       <forward name="success" path="/dgroom/displayAll.xsp"/>  
  17.     </action>  
  18. <!--第四种  就直接调用地址栏中的最后的那个 login()方法 -->  
  19.     <action path="/dgroom/login" type="com.dgroom.struts.BeanAction"  
  20.       name="accountBean" scope="session"  
  21.       validate="false">  
  22.       <forward name="success" path="/dgroom/index.xsp"/>  
  23.     </action>  

而在其它的Bean中的方法也如同下面的这个方法,都是返回一个字符串

而在AccountBean.java中的方法如下

java 代码
  1. public class AccountBean   
  2. {   
  3.     public String login(){   
  4.         if(){   
  5.             //成功   
  6.             return "success";   
  7.         }   
  8.         //失败   
  9.         return "failure";   
  10.     }   
  11. }   

下面的是通用的 BeanAction.java 类

java 代码
  1. package com.dgroom.struts;   
  2. import org.apache.struts.action.Action;   
  3. import org.apache.struts.action.ActionForm;   
  4. import org.apache.struts.action.ActionForward;   
  5. import org.apache.struts.action.ActionMapping;   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import java.lang.reflect.Method;   
  10.   
  11. import com.dgroom.util.*;   
  12.   
  13. public class BeanAction extends Action {   
  14.   
  15.   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)   
  16.       throws Exception {   
  17.   
  18.     String forward = "success";   
  19.     try {   
  20.         //这里是对ActionContext.save(request,reponse);   
  21.         //对映 Struts-config.xml 中的 第一种 :当请求中的 Form 为空时直接转 发   
  22.       if (form != null) {   
  23.         Method method = null;   
  24.         String methodName = mapping.getParameter();   
  25.   
  26.         //第三种:当参数中存在着某个值时并且不等于 * 时就直接调用那个参数中的方法   
  27.         if (methodName != null && !"*".equals(methodName)) {   
  28.           try {   
  29.             method = form.getClass().getMethod(methodName, null);   
  30.             forward = (String) method.invoke(form, null);   
  31.           } catch (Exception e) {   
  32.            // 这里是一个自定义的异常   
  33.           }   
  34.         }   
  35.         //第四种 当参数为空时 直接调用地址栏最后的方法   
  36.         if (method == null && !"*".equals(methodName)) {   
  37.           methodName = mapping.getPath();   
  38.           if (methodName.length() > 1) {   
  39.             int slash = methodName.lastIndexOf("/") + 1;   
  40.             methodName = methodName.substring(slash);   
  41.             if (methodName.length() > 0) {   
  42.               try {                
  43.                 method = form.getClass().getMethod(methodName, null);                  
  44.                 forward = (String) method.invoke(form, null);   
  45.               } catch (Exception e) {   
  46.                // 这里是一个自定义的异常   
  47.               }   
  48.             }   
  49.           }   
  50.         }   
  51.       }   
  52.   
  53.     } catch (Exception e) {   
  54.   
  55.       // 这里是一个自定义的异常   
  56.       throw e;   
  57.     }   
  58.   
  59.     return mapping.findForward(forward);   
  60.   }   
  61.   
  62. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值