【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我学spring3

先进行通用配置, 【第十章】集成其它Web框架 之 10.1 概述 

10.2  集成Struts1.x

10.2.1  概述

       Struts1.x是最早实现MVC(模型-视图-控制器)模式的Web框架之一,其使用非常广泛,虽然目前已经有Struts2.x等其他Web框架,但仍有很多公司使用Struts1.x框架。

       集成Struts1.x也非常简单,除了通用配置外,有两种方式可以将Struts1.x集成到Spring中:

  • 最简单集成:使用Spring提供的WebApplicationContextUtils工具类中的获取Spring Web容器,然后通过Spring Web容器获取Spring管理的Bean;
  • Struts1.x插件集成:利用Struts1.x中的插件ContextLoaderPlugin来将Struts1.x集成到Spring中。

 

       接下来让我们首先让我们准备Struts1x所需要的jar包:

1.1、从下载的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目录查找如下jar包,该jar包用于提供集成struts1.x所需要的插件实现等:

org.springframework.web.struts-3.0.5.RELEASE.jar 

      

 

1.2、从下载的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依赖jar包,该组jar是struts1.x需要的jar包:

com.springsource.org.apache.struts-1.2.9.jar                      //struts1.2.9实现包

com.springsource.org.apache.commons.digester-1.8.1.jar    //用于解析struts配置文件

com.springsource.org.apache.commons.beanutils-1.8.0.jar   //用于请求参数绑定

com.springsource.javax.servlet-2.5.0.jar                            //Servlet 2.5 API

antlr.jar                                                                          //语法分析包(已有)

commons-logging.jar                                                       //日志记录组件包(已有)

servlet-api.jar                                                                 //Servlet API包(已有)

jsp-api.jar                                                                      //JSP API包(已有,可选)

commons-validator.jar                                                 //验证包(可选)

commons-fileupload.jar                                               //文件上传包(可选)

 

  

10.2.2  最简单集成

只使用通用配置,利用WebApplicationContextUtils提供的获取Spring Web容器方法获取Spring Web容器,然后从Spring Web容器获取Spring管理的Bean。

 

1、 第一个Action实现:

 

java代码:
  1.       
  2. package cn.javass.spring.chapter10.struts1x.action;  
  3. import org.apache.struts.action.Action;  
  4. //省略部分import  
  5. public class HelloWorldAction1 extends Action {  
  6.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  7.    
  8.         WebApplicationContext ctx = WebApplicationContextUtils.  
  9.             getRequiredWebApplicationContext(getServlet().getServletContext());         
  10.         String message = ctx.getBean("message", String.class);  
  11.         request.setAttribute("message",  message);  
  12.         return mapping.findForward("hello");  
  13.     }  
  14. }  

       此Action实现非常简单,首先通过WebApplicationContextUtils获取Spring Web容器,然后从Spring Web容器中获取“message”Bean并将其放到request里,最后转到“hello”所代表的jsp页面。

 

2、JSP页面定义(webapp/WEB-INF/jsp/hello.jsp):

 

java代码:
  1. <%@ page language="java" pageEncoding="UTF-8"  
  2. contentType="text/html; charset=UTF-8" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  4. "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7.     <title>Hello World</title>  
  8. </head>  
  9. <body>  
  10.     ${message}  
  11. </body>  
  12. </html>  

 

3、配置文件定义:

 

3.1、Spring配置文件定义(resources/chapter10/applicationContext-message.xml):

在此配置文件中定义我们使用的“message”Bean;

 

java代码:
  1. <bean id="message" class="java.lang.String">  
  2.     <constructor-arg index="0" value="Hello Spring"/>  
  3. </bean>  

 

3.2、struts配置文件定义(resources/chapter10/struts1x/struts-config.xml):

 

java代码:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC  
  3.           "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"  
  4.           "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">  
  5. <struts-config>  
  6.   <action-mappings>  
  7.     <action path="/hello" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction1">  
  8.        <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  
  9.     </action>  
  10.   </action-mappings>  
  11. </struts-config>  

 

3.3、web.xml部署描述符文件定义(webapp/WEB-INF/web.xml)添加如下内容:

 

java代码:
  1. <!-- Struts1.x前端控制器配置开始   -->  
  2.        <servlet>  
  3.         <servlet-name>hello</servlet-name>  
  4.         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  5.          
  6.         <init-param>  
  7.             <param-name>config</param-name>  
  8.             <param-value>  
  9.                       /WEB-INF/classes/chapter10/struts1x/struts-config.xml  
  10.              </param-value>  
  11.         </init-param>  
  12.         <load-on-startup>1</load-on-startup>  
  13.        </servlet>  
  14.        <servlet-mapping>  
  15.         <servlet-name>hello</servlet-name>  
  16.         <url-pattern>*.do</url-pattern>  
  17.       </servlet-mapping>  
  18. <!-- Struts1.x前端控制器配置结束   -->  
  19.    

 

       Struts1.x前端控制器配置了ActionServlet前端控制器,其拦截以.do开头的请求,Strut配置文件通过初始化参数“config”来指定,如果不知道“config”参数则默认加载的配置文件为“/WEB-INF/ struts-config.xml”。

 

4、执行测试:在Web浏览器中输入http://localhost:8080/hello.do可以看到“Hello Spring”信息说明测试正常。

 

       有朋友想问,我不想使用这种方式,我想在独立环境内测试,没关系,您只需将spring/lib目录拷贝到spring/webapp/WEB-INF/下,然后将webapp拷贝到如tomcat中即可运行,尝试一下吧。

       Spring还提供ActionSupport类来简化获取WebApplicationContext,Spring为所有标准Action类及子类提供如下支持类,即在相应Action类后边加上Support后缀:

  • ActionSupport
  • DispatchActionSupport
  • LookupDispatchActionSupport
  • MappingDispatchActionSupport

具体使用方式如下:

 

1、Action定义

 

java代码:
  1. package cn.javass.spring.chapter10.struts1x.action;  
  2. //省略import  
  3. public class HelloWorldAction2 extends ActionSupport {  
  4. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  5.         WebApplicationContext ctx = getWebApplicationContext();      
  6.         String message = ctx.getBean("message", String.class);  
  7.         request.setAttribute("message", message);  
  8.         return mapping.findForward("hello");  
  9.     }  
  10. }  

 

和第一个示例唯一不同的是直接调用getWebApplicationContext()即可获得Spring Web容器。

 

2、修改Struts配置文件(resources/chapter10/struts1x/struts-config.xml)添加如下Action定义:

 

java代码:
  1. <action path="/hello2" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction2">  
  2.     <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  
  3. </action>  

 

3、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello2.do可以看到“Hello Spring”信息说明Struts1集成成功。

 

这种集成方式好吗?而且这种方式算是集成吗?直接获取Spring Web容器然后从该Spring Web容器中获取Bean,暂且看作是集成吧,这种集成对于简单操作可以接受,但更复杂的注入呢?接下来让我们学习使用Struts插件进行集成。

 

10.2.2  Struts1.x插件集成

Struts插件集成使用ContextLoaderPlugin类,该类用于为ActionServlet加载Spring配置文件。

 

1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中配置插件:

 

java代码:
  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  
  2.     <set-property property="contextClass" value="org.springframework.web.context.support.XmlWebApplicationContext"/>  
  3.     <set-property property="contextConfigLocation" value="/WEB-INF/hello-servlet.xml"/>  
  4.     <set-property property="namespace" value="hello"/>  
  5. </plug-in>  
  • contextClass:可选,用于指定WebApplicationContext实现类,默认是XmlWebApplicationContext;
  • contextConfigLocation:指定Spring配置文件位置,如果我们的ActionServlet 在 web.xml 里面通过 <servlet-name>hello</servlet-name>指定名字为“hello”,且没有指定contextConfigLocation,则默认Spring配置文件是/WEB-INF/hello-servlet.xml;
  • namespace:因为默认使用ActionServlet在web.xml定义中的Servlet的名字,因此如果想要使用其他名字可以使用该变量指定,如指定“hello”,将加载的Spring配置文件为/WEB-INF/hello-servlet.xml;

 

由于我们的ActionServlet在web.xml中的名字为hello,而我们的配置文件在/WEB-INF/hello-servlet.xml,因此contextConfigLocation和namespace可以不指定,因此最简单配置如下:

 

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

 

通用配置的Spring Web容器将作为ContextLoaderPlugin中创建的Spring Web容器的父容器存在,然而可以省略通用配置而直接在struts配置文件中通过ContextLoaderPlugin插件指定所有配置文件。

 

插件已经配置了,那如何定义Action、配置Action、配置Spring管理Bean呢,即如何真正集成Spring+Struts1x呢?使用插件方式时Action将在Spring中配置而不是在Struts中配置了,Spring目前提供以下两种方式:

  • 将Struts配置文件中的<action>的type属性指定为DelegatingActionProxy,然后在Spring中配置同名的Spring管理的Action Bean;
  • 使用Spring提供的DelegatingRequestProcessor重载 Struts 默认的 RequestProcessor来从Spring容器中查找同名的Spring管理的Action Bean。

看懂了吗?好像没怎么看懂,那就直接上代码,有代码有真相。

      

2、定义Action实现,由于Action将在Spring中配置,因此message可以使用依赖注入方式了:

 

java代码:
  1. package cn.javass.spring.chapter10.struts1x.action;  
  2. //省略  
  3. public class HelloWorldAction3 extends Action {  
  4.     private String message;  
  5.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {  
  6.    
  7.         request.setAttribute("message", message);  
  8.         return mapping.findForward("hello");  
  9.     }  
  10.     public void setMessage(String message) {//有setter方法,大家是否想到setter注入  
  11.         this.message = message;  
  12.     }  
  13. }  

 

3、DelegatingActionProxy方式与Spring集成配置:

 

3.1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中进行Action定义:

 

java代码:
  1. <action path="/hello3" type="org.springframework.web.struts.DelegatingActionProxy">  
  2.     <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  
  3. </action>  
  4.    

 

3.2、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定义Action对应的Bean:

 

java代码:
  1. <bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  
  2.     <property name="message" ref="message"/>  
  3. </bean>  

 

3.3、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello3.do可以看到“Hello Spring”信息说明测试正常。

       从以上配置中可以看出:

  • Struts配置文件中<action>标签的path属性和Spring配置文件的name属性应该完全一样,否则错误;
  • Struts通过DelegatingActionProxy去到Spring Web容器中查找同名的Action Bean;

很简单吧,DelegatingActionProxy是个代理Action,其实现了Action类,其内部帮我们查找相应的Spring管理Action Bean并把请求转发给这个真实的Action。

 

 

4、DelegatingRequestProcessor方式与Spring集成:

 

4.1、首先要替换掉Struts默认的RequestProcessor,在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中添加如下配置:

 

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

 

4.2、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中进行Action定义:

 

java代码:
  1. <action path="/hello4" type=" cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  
  2.     <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  
  3. </action>  

 

或更简单形式:

 

 

java代码:
  1. <action path="/hello4">  
  2.     <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>  
  3. </action>  

 

4.3、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定义Action对应的Bean:

 

java代码:
  1. <bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">  
  2.     <property name="message" ref="message"/>  
  3. </bean>  

 

4.4、启动嵌入式Web服务器并在Web浏览器中输入http://localhost:8080/hello4.do可以看到“Hello Spring”信息说明Struts1集成成功。

 

从以上配置中可以看出:

  • Struts配置文件中<action>标签的path属性和Spring配置文件的name属性应该完全一样,否则错误;
  • Struts通过DelegatingRequestProcessor去到Spring Web容器中查找同名的Action Bean;

很简单吧,只是由DelegatingRequestProcessor去帮我们查找相应的Action Bean,但没有代理Action了,所以推荐使用该方式。

 

图10-4 共享及专用Spring Web容器

 

Struts1x与Spring集成到此就完成了,在集成时需要注意一下几点:

  • 推荐使用ContextLoaderPlugin+DelegatingRequestProcessor方式集成;
  • 当有多个Struts模块时建议在通用配置部分配置通用部分,因为通用配置在正在Web容器中是可共享的,而在各个Struts模块配置文件中配置是不可共享的,因此不推荐直接使用ContextLoaderPlugin中为每个模块都指定所有配置,因为ContextLoaderPlugin加载的Spring容器只对当前的ActionServlet有效对其他ActionServlet无效,如图10-4所示。

原文地址:http://sishuok.com/forum/blogPost/list/2511.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值