Spring 与 Struts 的整合,主要有三种方式 : 

 


不管 Spring 用什么方式实现与 Struts 的整合 , 那么 Strtus 到底以什么样的方式装载 Spring 的应用环境呢?方法就是用使用 Sping 提供的 org.springframework.web.strtus.ContextLoaderPlugin ,在 Struts 的配置文件中注册 ContextLoaderPlugin 插件即可 . 代码如下 :


<plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
   value="/WEB-INF/applicationContext.xml" />
 </plug-in>


1. 通过 Spring 的 ActionSupport 类


1.1 因为要用到 Struts 的 MVC 实


现,所以 org.apache.struts.action.ActionServlet 是一定会用到的 . 所以,基于这几种整合方式 ,WEB-INF 下的 web.xml 还是不用更改的, ( 跟我们在以前不用 Spring 时的配置是一样的 )


<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
  </servlet>


1.2. 修改以前的的 Action 类,使其 extends 自 org.springframework.web.strtus.ActionSupport 类


1.3 在 Action 的 execute 方法中写 Spring 的代码以得到 ApplicationContext, 并跟据 Spring 配置文件中配置的 Bean (依赖注入的方式)


ApplicationContext context = this.getWebApplicationContext();
  HelloWorldService service = (HelloWorldService)
      context.getBean("helloWorldService");

注 : 这里我没有结合我的程序的业务进行讲解 , 你只需知道其中的原理然后自己通过实例来巩固一下 !


总结 : 这样简单的两步就可以把 Spring 与 Struts 整合到一起了 , 但缺点是很明显的 , 这种方式使 Spring 与 Struts 的代码耦合在一起,并且 Struts 不在 Spring 的控制之中 , 这样如果更换框架,或是想使用 Spring 的 AOP 都是比较困难的 , 如果多个动作放在一个 Actin 中,则这种方式就无能为力了 !


2. 通过 Spring 的 DelegatingRequestProccessor


       跟方式 1 相比,需要改动的代码处有几种 ,


2.1. 在 Struts 中,担任控制器角色的是 ActionServlet ,当有讲求发送至 ActionServlet 时, ActionServlet 的 doGet() 或 doPost() 方法会执行 RequestProccessor 的 process() 方法 .


   修改 Struts 的配置文件 strtus-config.xml , 还是像第一种方式那样注册 Spring 的 ContextLoaderPlugIn 插件 , 并使用 Spring 的 DegatingRequestProcessor 代替 Struts 的 RequestProcessor


<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
 <!-- 注册插件 -->
 <message-resources parameter="com.struts.ApplicationResources" />
 <plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
   value="/WEB-INF/applicationContext.xml" />
 </plug-in>

2.2. 将 Struts 中 XXXAction 的超类还原成 org.apache.struts.action.Action


2.3. 在用户自定义的 XXXAction 类中没有了 Spring 的得到 Bean 的代码,那么我们怎么


     与我们业务层取得联系呢?这里就可以通过依赖注入的方式 , 如 : 接口注入, setter 注入,构造注入。


小结 : 这种方法就不需要在 Action 里编写 Spring 的代码了,这种方式就解决了使用 ActionSupport 时两种框架代码耦合的缺点 . ,但这种方式还是有缺点的,那就是开发人员可以自己定义 RequestProcessor, 这样如果没有使用 Struts 默认的 RequestProcessor, 则需要手工来整合 Spring 和 Struts 了 .


3.


3.1. 同上两种方式 , 使用 DelegatingActionProxy 方式 ,WEB-INF 下的 web.xml 还是跟往常 做单一的 Struts 程序一样,另外还是加入 ContextLoaderPlugIn 插件


3.2. 将用户自定义的 XXXAction 类还是继承自 org.apache.struts.action.Action


<action-mapping/> 节点里的用户 <action> 的 class 属性修改成 :org.springframework.web.srtuts.DelegatingActionProxy


<action-mappings>
  <action attribute="loginForm" input="/login.jsp"
   name="loginForm" path="/login" scope="request"
   type="org.springframework.web.struts.DelegatingActionProxy">
   <forward path="/welcome.jsp" />
   <forward path="/login.jsp" />
  </action>
 </action-mappings>

总结 : 以上就 Spring 与 Struts 整合的三种方式进行了简单的讲解,由于代码量比较大,我并没有结合完整的程序进行讲解 . 如有任何疑问,请在博客里留言 , 我会尽快给予解释 .