posts - 11,  comments - 28,  trackbacks - 0

JpetStore中的Action与普通Struts的Action处理方式不一样。遍历JpetStore的src文件夹,并无一个具体的Action,那么它是如何来完成普通Struts的Action工作了?
查看JpetStore的Struts.xml可以发现,它的Action只有一个,即“org.apache.stuts.beanaction.Beanaction”。通过Eclipse查看beanaction.jar的源代码,可以发现Beanaction继承与普通Action,即具备普通的action的功能。那么它无具体Action的奥妙在哪,继续研究BeanAction的代码,截取BeanAction的excute方法中核心部分代码如下: 
/*遍历此方法的业务逻辑可知,*的优先级最高,如果是*,则不调用任何方法直接Forward,类似于ForwardAction*/ 
private static final String NO_METHOD_CALL = "*"
……. 
/*所有的FormBean都继承于BaseBean*/ 
BaseBean bean 
= (BaseBean) form; 
      ActionContext.initCurrentContext(request, response); 
      
if (bean != null
        
// Explicit Method Mapping 
/*下面是检查struts.xml配置中是否有parameter属性*/ 
        Method method 
= null
        String methodName 
= mapping.getParameter(); 
        
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) 
          
try 
/*通过反射,根据得到的方法名称取得方法的句柄*/ 
            method 
= bean.getClass().getMethod(methodName, null); 
            
synchronized (bean) 
/*下面是关键一句,调用basebean拥有的接口ActionInterceptor的实现DefaultActionInterceptor,来完成具体方法的调用*/ 
              forward 
= bean.getInterceptor().intercept(new ActionInvoker(bean, method)); 
            }
 
         …….. 
/*无Parameter属性,检查path路径的最后一个/后的名称,即为调用的方法名*/ 
        
// Path Based Method Mapping 
        if (method == null && !NO_METHOD_CALL.equals(methodName)) 
          methodName 
= mapping.getPath(); 
          
if (methodName.length() > 1
            
int slash = methodName.lastIndexOf("/"+ 1
            methodName 
= methodName.substring(slash); 
            
if (methodName.length() > 0
              
try 
                method 
= bean.getClass().getMethod(methodName, null); 
                
synchronized (bean) 
                  forward 
= bean.getInterceptor().intercept(new ActionInvoker(bean, method)); 
                }
 
             …….. 
/*根据调用方法返回的String,得到页面的转移路径*/ 
return mapping.findForward(forward); 
 
通过研究上面这段代码,我们可知,JpetStore中没有具体Action实现的关键原因即在于下面这几句
/*通过反射,根据得到的方法名称取得方法的句柄*/ 
            method 
= bean.getClass().getMethod(methodName, null); 
            
synchronized (bean) 
/*下面是关键一句,调用basebean拥有的接口ActionInterceptor的实现DefaultActionInterceptor,来完成具体方法的调用*/ 
              forward 
= bean.getInterceptor().intercept(new ActionInvoker(bean, method)); 
            }
 
即将原来Action中的excute方法的实现转移到FormBean中,这样实现显得更为简捷,方便。研究ActionInvoke,它的核心代码如下:
public String invoke() 
    
try 
      
return (String) method.invoke(bean, null); 
    }
 catch (Exception e) 
      
throw new BeanActionException("Error invoking Action.  Cause: " + e, e); 
    }
 
  }
 
至此可知,它调用的是formbean中的函数。且从这段代码可知,formbean的这类特殊函数,此处称为action方法,要符合两个特征:1)无参数;2)返回值为string,此返回string即是Struts-config.xml的全局或局部的forward。
以上是整个beanaction的实现机制。个人感觉此种实现方法对于开发者而言已经类似于ASP.NET的.aspx与.cs开发模式了。下面是通过实例来说明一下BeanAction如何控制formbean的
 
Struts-config.xml的配置里有3种映射方式,来告诉BeanAction把控制转到哪个form bean对象的哪个方法来处理。
(1)parameter=”*’直接跳转;(2)Parameter中含具体的方法名;(3)Path中最后一个/后的方法名
以这个请求连接为例http://localhost/jpetstore4/shop/viewOrder.shtml
1. URL Pattern

   
<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
    name
="orderBean" scope="session"
    validate
="false">
    
<forward name="success" path="/order/ViewOrder.jsp"/>
  
</action>

 
此种方式表示,控制将被转发到"orderBean"这个form bean对象 的"viewOrder"方法(行为)来处理。方法名取"path"参数的以"/"分隔的最后一部分。
2. Method Parameter 
<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
    name
="orderBean" parameter="viewOrder" scope="session"
    validate
="false">
    
<forward name="success" path="/order/ViewOrder.jsp"/>
  
</action>

 
此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。配置中的"parameter"参数表示form bean类上的方法。"parameter"参数优先于"path"参数。
3. No Method call
 <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
    name
="orderBean" parameter="*" scope="session"
    validate
="false">
    
<forward name="success" path="/order/ViewOrder.jsp"/>
  
</action>

此种方式表示,form bean上没有任何方法被调用。如果存在"name"属性,则struts把表单参数等数据填充到form bean对象后,把控制转发到"success"。否则,如果name为空,则直接转发控制到"success"。
这就相当于struts内置的org.apache.struts.actions.ForwardAction的功能
<action path="/shop/viewOrder" type="org.apache.struts.actions.ForwardAction"
    parameter
="/order/ViewOrder.jsp " scope="session" validate="false">
 
</action>
 

 
posted on 2007-01-12 16:34 涤生 阅读(3869) 评论(8)  编辑  收藏


FeedBack:
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2007-01-12 18:46 | 小武藏
scope="session"
不知道为什么要使用session呢?request不行么?
  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2007-01-13 11:46 | 江南白衣
好文,涤生的分析方便了一大堆觉得BaseAction非常棒,又懒得去看去理解源码,没有理解之前又不敢用它的懒人,比如我:)  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean[未登录]
2007-01-14 22:31 | jooroo
好文!不知为何beanaction包只是试验性质的用一用,是否有些什么defect我们不知道?  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2007-01-15 13:21 | 涤生
beanaction,这个是否有defect,不清楚了。我只是在学习这个JpetStore,把学习时的疑惑给理清楚。具体的beanaction有什么defect,需要深入的研究、使用才知道。  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2007-01-15 13:22 | 涤生
为什么使用Session不使用request,这个应该是由于它的实际需求+统一风格决定的。其实很多地方是可以使用request的  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2007-01-16 12:54 | 小武藏
谢谢涤生,我明白了。
  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2008-01-22 22:27 | 庞宏涛
我觉得这种方式有性能的损失,struts不是线程安全的,通过这种方式的话必须要求synchronized,也就是在一个时刻只能有一个用户执行 forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
其他的都要排队等待,在大并发量的系统上肯定不行,这种也只能做做例子什么的。  回复  更多评论
  
# re: iBatis的JpetStore示例中MVC机制实现的研究,BeanAction,BaseBean
2008-01-28 18:03 | it942
怎么现在JpetStore又用BaseAction了。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(5)

随笔档案

UML

搜索

  •  

最新评论

阅读排行榜

评论排行榜