Spring3 和 Struts2 整合

这不是类图,用Rose画得示意图

Spring3&Struts2整合

整合 Spring Strust2一起工作

需要的包列表:

Struts2:

  aopalliance-1.0.jar
  commons-beanutils-1.8.0.jar
  commons-collections-3.1.jar
  commons-digester-2.0.jar
  commons-fileupload-1.2.2.jar
  commons-io-2.0.1.jar
  commons-lang-2.4.jar
  commons-lang3-3.1.jar
  commons-logging-1.1.1.jar
  freemarker-2.3.19.jar
  javassist-3.11.0.GA.jar
  ognl-3.0.5.jar
  struts2-core-2.3.3.jar
  struts2-spring-plugin-2.3.3.jar
  xwork-core-2.3.3.jar

Spring3:

spring-aop-3.2.0.M1.jar
spring-asm-3.2.0.M1.jar
spring-beans-3.2.0.M1.jar
spring-context-3.2.0.M1.jar
spring-core-3.2.0.M1.jar
spring-expression-3.2.0.M1.jar
spring-jdbc-3.2.0.M1.jar
spring-orm-3.2.0.M1.jar
spring-tx-3.2.0.M1.jar
spring-web-3.2.0.M1.jar

 

Struts2中整合SpringIoC支持是一件十分简单的事情。让我们一步一步来实现:
1
)复制struts2-spring-plugin-x-x-x.jar和相应的spring.jar/WEB-INF/lib目录下。
2
)在struts.properties中设置struts.objectFactory属性值

struts.properties

1. struts.objectFactory = spring  

或者是在XML文件中进行常量配置

struts.xml

1. <struts>  

2.     <constant name="struts.objectFactory" value="spring" />  

3. </struts>  

3)配置Spring监听器

web.xml

1.<listener>  

2.    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

3.</listener>  

通过Spring配置来注册对象

applicationContext.xml

1. <?xml version="1.0" encoding="UTF-8"?>  

2. <!DOCTYPE beans PUBLIC    

3.     "-//SPRING//DTD BEAN//EN"   

4.     "http://www.springframework.org/dtd/spring-beans.dtd">  

5. <beans default-autowire="autodetect">  

6.     <bean id="hello" class="hpfyeah.struts2.spring.HelloWorldAction"/>  

7. </beans>  

如果有多个Spring配置文件(默认情况下:唯一的一个applicationContext.xml放在WEB-INF目录下的话,无需配置)则需要在web.xml中进行下列设置,从而使SpringApplicationContext通过匹配所给定模式的文件来初始化对象

web.xml

1. <!-- 用来定位Spring XML文件的上下文配置 -->  

2. <context-param>  

3.     <param-name>contextConfigLocation</param-name>  

4.     <param-value>

5.         /WEB-INF/applicationContext.xml, /WEB-INF/applicationContext2.xml

6.     </param-value>  

7. </context-param>  

  

这里注意:

1. 设置监听器的位置放在文件位置配置之后

 

4) 若果你的struts.xml 放在/WEB-INF目录下的话, 则要在Struts2的过滤器中配置如下:

 <filter>
    <filter-name>struts2</filter-name>
    <filter-class>     
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>   
    <init-param>
      <description>我的struts配置文件放在WEB-INF根目录下,文件名为action.xml</description>
     <param-name>config</param-name>
     <param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
  </init-param>    
 </filter>

5)修改你的Struts配置文件

struts.xml

1. <!DOCTYPE struts PUBLIC      

2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      

3.     "http://struts.apache.org/dtds/struts-2.0.dtd">     

4. <struts>     

5.     <include file="struts-default.xml"/>     

6.     <package name="default" extends="struts-default">     

7.         <action name="pureStruts" class="hpfyeah.struts2.spring.HelloWorldAction">

8.             <result>hello.jsp</result>     

9.         </action>  

10.        <action name="springStruts" class="hello">      

11.            <result>hello.jsp</result>     

12.        </action>     

13.</struts>  

默认情况下,Spring从上面显示的applicationContext.xml文件中寻找为hello所做的配置

5)好了,现在你的Struts2Spring就能正常的一起工作了。

 

有几个配置技术点需要详细说明下:
装配模式。你可以通过设置修改struts.properties中下列属性的值来改变装配模式。

name

按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的

type

按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean

auto

Spring会试图自动监测来找到最好的方法自动装配你的action

constructor

Spring会自动装配bean的构造函数的参数

是否使用类缓存。你可以通过设置修改struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为truefalse,默认为true

struts.properties

1. struts.objectFactory.spring.useClassCache = false 

 

 

 

posted on 2012-09-07 16:08  babyblue  阅读(274)  评论(0)    收藏  举报