`
famoushz
  • 浏览: 2998381 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iBatis的JpetStore中的BeanAction的研究

阅读更多
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方法中核心部分代码如下: 
 
private static final String NO_METHOD_CALL = "*"; 
……. 
 
BaseBean bean = (BaseBean) form; 
      ActionContext.initCurrentContext(request, response); 
      if (bean != null) { 
        // Explicit Method Mapping 
 
        Method method = null; 
        String methodName = mapping.getParameter(); 
        if (methodName != null && !NO_METHOD_CALL.equals(methodName)) { 
          try { 
 
            method = bean.getClass().getMethod(methodName, null); 
            synchronized (bean) { 
 
              forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method)); 
            } 
         …….. 
 
        // 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)); 
                } 
             …….. 
 
return mapping.findForward(forward); 
  
通过研究上面这段代码,我们可知,JpetStore中没有具体Action实现的关键原因即在于下面这几句 
 
            method = bean.getClass().getMethod(methodName, null); 
            synchronized (bean) { 
 
              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>
 
分享到:
评论

相关推荐

    jpetstore4.0 (spring+struts+ibatis)

    在jpetstore4.0中,iBatis主要负责: 1. **SQL动态生成**:iBatis允许根据传入的参数动态生成SQL语句,避免硬编码SQL,提高了代码的可读性和可维护性。 2. **数据访问抽象**:通过配置文件,iBatis将数据库操作与...

    jpetstore系统架构图

    iBatis(现称MyBatis)是一个半自动化的ORM(对象关系映射)框架,它在jpetstore系统中用于处理数据持久化操作。通过DaoManager创建的SqlMapDao实例,系统能够高效地执行SQL语句,与数据库进行交互。此外,...

    jpetstore spring 的经典完整可直接运行的例子 jpetstore

    在这个例子中,你可以看到如何使用Spring配置文件管理Bean,定义Bean之间的依赖关系,以及如何在Struts Action中通过@Autowired注解自动注入依赖。同时,iBatis的SQL映射文件展示了如何编写动态SQL和结果映射,以...

    struts+spring+ibatis.doc

    通过整合Struts、Spring与ibatis,JpetStore 4.0不仅展现了ibatis在数据持久化方面的高效与灵活性,也展示了Struts与Spring在构建高效、可维护的J2EE应用程序中的协同优势。这种模式下的开发,不仅减少了代码量,...

    struts+spring+ibatis框架总结.doc

    然而,传统的Struts开发模式可能会导致过多的Action类和Form Bean,JpetStore 4.0则采取了创新的方式,仅使用一个自定义Action类和简化form bean的定义,降低了复杂性。 Spring Framework的核心功能包括AOP(面向切...

    Struts+Spring+Ibatis整合开发

    在架构上,JpetStore 4.0的核心特点是只有一个通用的BeanAction类,它通过反射机制根据请求URL调用对应的Form Bean方法。这种方式降低了对Struts的依赖,使得代码更简洁,同时也便于维护和扩展。 总体来说,Struts+...

Global site tag (gtag.js) - Google Analytics