[文档中]Struts与Spring整合

要将 Struts 与 Spring 集成,你有两个选择:

  • 配置 Spring 将 Action 作为 bean 托管,使用 ContextLoaderPlugin, 并且在 Spring context中设置依赖关系。

  • 继承 Spring 的 ActionSupport 类并且 使用getWebApplicationContext() 方法获取 Spring 管理的 bean。

15.4.1. ContextLoaderPlugin

ContextLoaderPlugin 是 Struts 1.1+ 的插件,用来为 Struts 的 ActionServlet 加载 Spring context文件。 这个context引用 WebApplicationContext (由 ContextLoaderListener 加载) 作为它的父类。默认的context文件是映射的 Servlet 的名字,加上 -servlet.xml后缀。 如果 ActionServlet 在 web.xml 里面的定义是 <servlet-name>action</servlet-name>, 那么默认的文件就是 /WEB-INF/action-servlet.xml

要配置这个插件,请把下面的 XML 贴到 struts-config.xml 文件中 plug-ins 部分的底端:

 
 
xml 代码
  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>  

context配置文件的位置可以通过 contextConfigLocation属性来自定义。

 
 
xml 代码
  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  
  2.   <set-property property="contextConfigLocation"  
  3.       value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>  
  4. </plug-in>  

你也可以使用这个插件加载所有的配置文件,这在使用测试工具(例如 StrutsTestCase)的时候特别有用。 StrutsTestCase 的 MockStrutsTestCase 不会在启动的时候初始化 Listener, 将你所有的配置文件放在plug-in里面是一种解决方案。(有个 已记录的 bug 就是针对这个问题的,但是已经被标记为“无须改正”)。

struts-config.xml 中配置好插件以后,你可以配置Sping来管理 Action。Spring (1.1.3以后的版本) 提供下面两种方式:

  • 用 Spring 的DelegatingRequestProcessor重载 Struts 默认的 RequestProcessor

  • <action-mapping>type 属性设为 DelegatingActionProxy

这两种方法都允许你在 action-context.xml 文件中管理你的 Action 以及依赖关系。 连接 struts-config.xmlaction-servlet.xml 中的 Action 的桥梁 是 action-mapping 的“path”和 bean 的“name”。如果你在 struts-config.xml 文件中有如下配置:

 
 
xml 代码
  1. <action path="/users" .../>  

你必须在 action-servlet.xml 中将 Action bean 的名字定义为 “/users”:

 
 
xml 代码
  1. <bean name="/users" .../>  
15.4.1.1. DelegatingRequestProcessor

为了在 struts-config.xml 文件中配置 DelegatingRequestProcessor,你需要重载 <controller> 元素的 “processorClass” 属性。 下面的几行应该放在 <action-mapping> 元素的后面。

 
  
xml 代码
  1. <controller>  
  2.   <set-property property="processorClass"  
  3.       value="org.springframework.web.struts.DelegatingRequestProcessor"/>  
  4. </controller>  

增加这些设置之后,不管你查询任何类型的 Action,Sping都自动在它的context配置文件中寻找。 实际上,你甚至不需要指定类型。下面两个代码片断都可以工作:

 
  
xml 代码
  1. <action path="/user" type="com.whatever.struts.UserAction"/>           
  2. <action path="/user"/>  

如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为 <action path="/user"/>,而且它的 module 前缀为“admin”, 那么它应该对应名为 <bean name="/admin/user"/> 的 bean。

注意

如果你在 Struts 应用中使用了 Tiles,你需要配置 <controller> 为 DelegatingTilesRequestProcessor

15.4.1.2. DelegatingActionProxy

如果你有一个自定义的 RequestProcessor 并且不能够使用 DelegatingRequestProcessor 或者 DelegatingTilesRequestProcessor,你可以使用 DelegatingActionProxy 作为你 action-mapping 中的类型。

 
  
xml 代码
  1. <action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"  
  2.     name="userForm" scope="request" validate="false" parameter="method">  
  3.   <forward name="list" path="/userList.jsp"/>  
  4.   <forward name="edit" path="/userForm.jsp"/>  
  5. </action>  

action-servlet.xml 文件中的bean定义依然不变,不管你使用了自定义的 RequestProcessor 还是 DelegatingActionProxy

如果你把 Action 定义在Spring的context文件里,那么 Spring bean 容器的所有特性都可用了: 比如,依赖注入,再比如,为每个请求初始化一个新的 Action 实例。 如果要使用这个特性, Action bean 定义中需要声明scope="prototype"

 
  
xml 代码
  1. <bean name="/user" scope="prototype" autowire="byName"  
  2.     class="org.example.web.UserAction"/>  

15.4.2. ActionSupport

正如前面提到的,你可以使用 WebApplicationContextUtils 类从 ServletContext 中获得 WebApplicationContext 。 另一个简单的办法是继承 Spring 的 Action 类。举个例子,除了继承 Struts 的 Action 之外,你也可以继承 Spring 的 ActionSupport 类。

ActionSupport 类提供了一些便利的方法,例如 getWebApplicationContext()。 下面的例子展示了如何在 Action 中使用它:

public class UserAction extends DispatchActionSupport {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method...");
        }
        WebApplicationContext ctx = getWebApplicationContext();
        UserManager mgr = (UserManager) ctx.getBean("userManager");
        // talk to manager for business logic
        return mapping.findForward("success");
    }
}

Spring 包含了所有标准 Struts Action 的子类 - Spring 版本在类名末尾附加了 Support

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值