`

让Struts 1焕发青春----小议对Struts的改造.

阅读更多

转载自:http://www.iteye.com/topic/93984

目前流行的新型的MVC框架 几乎都在"增强单元测试能力"上做了很多文章.
目的就是让 Controller 可以脱离web容器单独进行单元测试.

大多数采用的方法都是使 Controller 中的方法的参数 和 返回值 与 j2ee特有的类(如HttpXXXX)无关.

例如 传入的 是若干个 HashMap ,返回的是纯的字符串.
这样在单元测试的时候,只要new一个Controller,准备一些装有测试数据的Map,
然后执行相关的Controller方法,再然后看看返回值,就可以了.

显然,流行的Struts 1.X (>=1.2) 是不具备这样的特性的.
如果您现在有一个遗留的使用Struts 1.X(>=1.2) 的系统,或者因为人力资源的原因,不能立即引入ww或Struts2.x,
那么如何让您的Action类似的可测性了呢(其实除了可测试性,这样的改进往往也会大大的减少代码的开发量).

下面我就把我的一些心得写出来,和大家一起分享,欢迎大家拍砖.
注意:以下讨论均以改造 DispatchAction 为例(不使用DispatchAction的情况应该比较少吧 呵呵).

先说明一下改造的目的:
简化开发,减少代码量
增强单元测试能力
全面兼容现有DispatchAction(这点很重要)

一个传统的 DispatchAction 代码结构

代码
  1. public class MyClassicAction extends DispatchAction {   
  2.     // 一个添加user的操作   
  3.     public ActionForward addUser(ActionMapping mapping, ActionForm form,   
  4.             HttpServletRequest request, HttpServletResponse response)   
  5.             throws Exception {   
  6.         // 1 取得参数 ...   
  7.         // 2 执行相关的BO DAO方法   
  8.         // 3 取得执行后的返回值   
  9.            
  10.          if (添加成功) {   
  11.             return mapping.findForward("Succee");   
  12.          }   
  13.            
  14.         return mapping.findForward("Fail");   
  15.         // 还有一种情况是 返回 new ActionForward(....).   
  16.     }   
  17. }   
<script>render_code();</script>
对于上面的代码大家再熟悉不过了,我们现在要做的事情就是通过改造,让上面的action方法变成下面的样子.

 

 

代码
  1. // 一个添加user的操作   
  2. public String addUser( 传入与web容器无关的参数 )   
  3.         throws Exception {   
  4.     // 1 取得参数 ...   
  5.     // 2 执行相关的BO DAO方法   
  6.     // 3 取得执行后的返回值   
  7.        
  8.      if (添加成功) {   
  9.         return "Succee";   
  10.      }   
  11.        
  12.     return "Fail";   
  13.     // 对于action中返回 new ActionForward(....).的情况,我们可以设定另外一种特殊的前缀.   
  14.     // 例如  return "NEW:/addUserFail.jsp";   
  15. }   
<script>render_code();</script>

 

上面的代码是一个最终的目标,我们先一步一步来.
首先这种改造我们一定要从 Struts 提供的 DispatchAction 做起.
总体思路很明显:
自己写个类 继承 DispatchAction ,并且添加或修改一些方法, 然后项目中的其他的DispatchAction都继承这个自己写的DispatchAction类.

简单说一下Struts 的 DispatchAction的工作流程.

首先 它也是一个action,所以请求来了 他会自动去执行execute方法
execute方法内部做一些操作后,会调用 dispatchMethod 方法
dispatchMethod方法会,调用getMethod方法 来取得欲执行的方法,并且执行之

改造的核心就是围绕最关键的dispatchMethod方法.
而由于我们要"全面兼容现有DispatchAction",所以对于getMethod我们不做修改,而是选择增加一个类似的方法 getMethodTD

首先写一个新的 .DispatchAction基类,他继承org.apache.struts.actions.DispatchAction.

 

代码
  1.   
  2. public class TDDispatchAction extends DispatchAction {   
  3.        
  4.     protected Class[] typesTD = {TDServletWrapper.class };   
  5.        
  6.     protected final static String TD_METHODCACHE_PREFIX="TD_METHODCACHE_PREFIX";   
  7.     protected final static String TD_DEFAULT_METHOD="defaultMethod";   
  8.     protected final static String TD_NEW_FORWARD_PREFIX="NEW:";   
  9.        
  10.   
  11.        
  12.     protected Method getMethodTD(String name)   
  13.         throws NoSuchMethodException {   
  14.   
  15.         String cacheKey=TD_METHODCACHE_PREFIX+name;   
  16.            
  17.         synchronized (methods) {   
  18.             Method method = (Method) methods.get(cacheKey);   
  19.   
  20.             if (method == null) {   
  21.                 method = clazz.getMethod(name, typesTD);   
  22.                 methods.put(cacheKey, method);   
  23.             }   
  24.   
  25.             return (method);   
  26.         }   
  27.     }   
  28.   
  29. // 对unspecified这个方法的扩展实际上与本次讨论无关,只是为我们框架加的一个小功能   
  30. // 提供一个默认的方法.也就是说,如果您的action中,有名为 defaultMethod 的方法,那么无需在请求的参数中加入相关信息,就会自动执行.   
  31.     protected ActionForward unspecified(ActionMapping mapping, ActionForm form,   
  32.             HttpServletRequest request, HttpServletResponse response)   
  33.             throws Exception {   
  34.         return dispatchMethod(mapping, form, request, response, TD_DEFAULT_METHOD);   
  35.     }   
  36.   
  37.    protected ActionForward dispatchMethod(ActionMapping mapping,   
  38.         ActionForm form, HttpServletRequest request,   
  39.         HttpServletResponse response, String name)   
  40.         throws Exception {   
  41.     // 后面再说   
  42.   
  43.     }   
  44.  }   
  45.   
<script>render_code();</script>

 

首先来看一下这个 getMethodTD 方法.
原getMethod(String name)方法的作用是:
取得一个名字为name,参数类型为 ActionMapping , ActionForm , HttpServletRequest ,HttpServletResponse 的一个方法.

而这个新写的 getMethodTD 方法的作用是:
取得一个名字为name,参数类型为 TDServletWrapper 的一个方法.
这个大家和 原getMethod一对比就能看出来作用和差异了,注意那个cacheKey=TD_METHODCACHE_PREFIX+name,这个是必须的,不能直接用name做key.

其中这个 TDServletWrapper 就是封装的一个"可以"与web容器脱离的对象,注意,这里用的是可以.而不是直接就脱离了.
对此后面再做解释.

要添加的 getMethodTD 方法添加完了.
下面来看一下重点 改造 dispatchMethod方法:

原dispatchMethod方法的流程其实很简单:
1 调用 getMethod(name)方法,取得真正要执行的方法 并执行(传入的参数类型为 ActionMapping , ActionForm , HttpServletRequest ,HttpServletResponse ).
2 返回 方法执行的 结果 (返回结果类型为 ActionForward ).

改造后的目的是:

1 调用 getMethodTD(name)方法,取得真正要执行的方法并执行(传入的参数类型为TDServletWrapper ).
2 取得 方法执行的 结果 (返回结果类型为 String ).
3 根据这个返回的String, 创建ActionForward对象,并返回

但是我们还有一个目的是"兼容",所以最后确定的流程是

1 调用 getMethod(name)方法,取得真正要执行的方法,如果取得,则按原方式继续.
2 如果没有取得 调用 getMethodTD(name)方法,取得真正要执行的方法,若取得则执行.
3 根据返回结果的类型不同 做不同的操作.

 

代码
  1. // 改造的 dispatchMethod方法,重点看 //TD...字样所在的代码段   
  2.     protected ActionForward dispatchMethod(ActionMapping mapping,   
  3.         ActionForm form, HttpServletRequest request,   
  4.         HttpServletResponse response, String name)   
  5.         throws Exception {   
  6.   
  7.         if (name == null) {   
  8.             return this.unspecified(mapping, form, request, response);   
  9.         }   
  10.   
  11.         Method method = null;   
  12.            
  13. //TD:   Modified by Wei Zijun   
  14.         boolean useTDMethod=false;   
  15.         try {   
  16.             // 按传统方式取 action方法   
  17.             method = getMethod(name);   
  18.         } catch (NoSuchMethodException ex) {   
  19.             try {   
  20.             // 没取到的话 再按新方式取 action方法   
  21.              method = getMethodTD(name);   
  22.              // 标识一下到底是使用的原始方法 还是新方法   
  23.              useTDMethod=true;   
  24.             } catch (NoSuchMethodException e) {   
  25.                 String message =   
  26.                     messages.getMessage("dispatch.method", mapping.getPath(), name);   
  27.        
  28.                 log.error(message, e);   
  29.        
  30.                 String userMsg =   
  31.                     messages.getMessage("dispatch.method.user", mapping.getPath());   
  32.                 throw new NoSuchMethodException(userMsg);   
  33.             }   
  34.                
  35.         }   
  36.   
  37.         ActionForward forward = null;   
  38.   
  39.         try {   
  40. //TD:   Modified by Wei Zijun   
  41.             Object forwardR=null;   
  42.                
  43.             if (!useTDMethod){   
  44.             // 如果是原始 action 方法   
  45.                 Object[] args = { mapping, form, request, response };   
  46.                 forwardR= method.invoke(this, args);   
  47.                    
  48.             }else{   
  49.             // 如果是新 action 方法   
  50.                 // TDServletWrapperFactory.getInstance创建一个封装的对象   
  51.                 Object[] args = { TDServletWrapperFactory.getInstance(mapping, form,request, response) };   
  52.                 forwardR= method.invoke(this, args);   
  53.             }   
  54.                
  55.             if (forwardR!=null){   
  56.                 if (forwardR instanceof ActionForward){   
  57.                     forward=(ActionForward)forwardR;   
  58.                 }else{   
  59.                     // 如果返回值是字符串:   
  60.                     // 以 TD_NEW_FORWARD_PREFIX("NEW:")开头,则new ActionForward;   
  61.                     // 否则 调用 mapping.findForward.   
  62.                     if ( (String.valueOf(forwardR) ).toUpperCase().startsWith(TD_NEW_FORWARD_PREFIX)){   
  63.                         forward = new ActionForward(String.valueOf(forwardR).substring(TD_NEW_FORWARD_PREFIX.length()) );   
  64.                     }else{   
  65.                         forward = mapping.findForward(String.valueOf(forwardR));   
  66.                     }   
  67.                 }   
  68.             }   
  69.                
  70.         } catch (ClassCastException e) {   
  71.             String message =   
  72.                 messages.getMessage("dispatch.return", mapping.getPath(), name);   
  73.   
  74.             log.error(message, e);   
  75.             throw e;   
  76.         } catch (IllegalAccessException e) {   
  77.             String message =   
  78.                 messages.getMessage("dispatch.error", mapping.getPath(), name);   
  79.   
  80.             log.error(message, e);   
  81.             throw e;   
  82.         } catch (InvocationTargetException e) {   
  83.   
  84.             Throwable t = e.getTargetException();   
  85.   
  86.             if (t instanceof Exception) {   
  87.                 throw ((Exception) t);   
  88.             } else {   
  89.                 String message =   
  90.                     messages.getMessage("dispatch.error", mapping.getPath(),   
  91.                         name);   
  92.   
  93.                 log.error(message, e);   
  94.                 throw new ServletException(t);   
  95.             }   
  96.         }   
  97.   
  98.         return (forward);   
  99.     }   
  100.   
<script>render_code();</script>

 

这样做完后,我们的action就可以支持下面的任何一种类型的方法了:

 

代码
  1. // 传统方式   
  2. public ActionForward testSubmit(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception;   
  3.   
  4. // 新方式   
  5. public String testSubmit1(TDServletWrapper servletWrapper) throws Exception;   
  6.   
  7. // 以下两者是则中方式.   
  8. public String testSubmit2(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception;   
  9.   
  10. public ActionForward testSubmit3(TDServletWrapper servletWrapper) throws Exception;   
  11.   
<script>render_code();</script>

 

下面简单说一下 TDServletWrapper .
这个类实际上就是封装了 ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response,
同时提供了一些用来简化开发的方法,例如

public String getParameter(String key,String ifNull);
public Integer getIntegerParameter(String key);
public void bind(Object bean);
等等.
不再具体叙述,看一下代码就应该都明白了.

TDServletWrapper 是一接口,他有两个实现:
TDServletWrapperTestImpl 为测试专用的,是一个与web容器无关的实现.
他主要就是利用"模拟"request response的方式来处理
目前他的实现还不是很完全(例如没有模拟session等),大家可以根据自己的需求来补充.

大家也可以根据这个原理 不用TDServletWrapper ,而完全的使用Map等,
总之,通过dispatchMethod的改造,几乎可以实现你全部的合理的想法(别超出Struts1的能力极限就行),我在这里提供的只是一个思路而已.

经过上面的改造,一个可测试的action就诞生了:

 

代码
  1. public class MyClassicAction extends TDDispatchAction {   
  2.     // 一个添加user的操作   
  3.     public ActionForward addUser(TDServletWrapper servletWrapper) throws Exception {   
  4.            
  5.         UserInfoVO vo=new UserInfoVO();   
  6.         servletWrapper.bind(vo);   
  7.        
  8.         // 校验,并执行相关DAO操作   
  9.   
  10.          if ( 添加成功 ) {   
  11.             return "Succee";   
  12.          }   
  13.            
  14.         return "Fail";   
  15.   
  16.     }   
  17. }   
<script>render_code();</script>

 

测试的可以这样写:

代码
  1. public static void testAddUser() throws Exception {   
  2.     // 创建一个action对象   
  3.   
  4.     TestAction test=new TestAction();   
  5.   
  6.     //准备一些测试数据 放入map里,模仿request的Parameters   
  7.     Map requestParameters=new HashMap();   
  8.        
  9.     requestParameters.put("userName""asdqe");   
  10.     requestParameters.put("password""mghjghj");   
  11.     requestParameters.put("userRole""1");   
  12.     requestParameters.put("email""ut_email");   
  13.     requestParameters.put("gender""1");   
  14.     requestParameters.put("age""22");   
  15.        
  16.     // TDServletWrapperFactory.getTestInstance 方法用来取得一个脱离web容器的 TDServletWrapper对象 )   
  17.     Assert.isTrue("Sucess".equals(test.testSubmit1(TDServletWrapperFactory.getTestInstance(requestParameters))) );   
  18.   
  19. }   
<script>render_code();</script>

 

至此全部改造完毕.
这样的改造对于大家来说也许没什么意义,不过多少也是一种尝试.
由于Struts1.X(>=1.2)的 局限性,以及我个人能力的局限性,很多改造还不够彻底 不够合理,
对于前者我无能为力,对于后者,还恳请大家多多指正我的不足 谢谢了先.

上次发了一篇对 spring jdbctemplate的改造,这次又发了一篇对 struts 1 的改造,
(其实我那个ecside 也是对老版extremecomponents的改造)
也许很多人会觉得我太落伍了,这些老掉牙的东西还改造个什么劲儿啊 呵呵
其实,很多时候,对于我来说,改造就是一个智力游戏,在一些现有的东西实现一些自己的新的想法,这样的感觉总是快乐的.

而且,我们的项目就是用的老技术 老东西,在不能换新东西的情况下,努力的挖掘现有东西的剩余价值,也许更具有实际意义.

  • action.rar (4.7 KB)
  • 描述: 源码,不包括示例代码
  • 下载次数: 9
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics