Struts2+Spring整合

Struts2和Spring整合
   需要JAR包:
      Struts2:
        1、struts2-core-2.0.11.jar
        2、xwork-2.0.4.jar 
        3、commons-logging-1.0.4.jar
        4、freemarker-2.3.8.jar  
        5、ognl-2.6.11.jar
        6、struts2-spring-plugin-2.0.9.jar(这个JAR包要和Spring的版本对应上,否则会有不必要的报错)
      Spring:
         这里只使用了核心包
         1、spring.jar
   配置文件:
       Src目录下的Struts的配置文件
          1、struts.xml
          2、struts.properties
       WEB-INF目录下,只需要放置Spring的配置文件 
          1、applicationContexxt.xml
          2、web.xml
  
   配置文件的内容: 
      1、配置web.xml,使得web应用支持Struts2
   <!--配置Struts2文件位置  -->
       <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
       </filter>

       <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
2、配置使得项目启动的时候,加载spring配置文件
   <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/applicationContext.xml</param-value> 
   </context-param> 
    <listener> 
                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
3、配置Struts2的struts.properties
   配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:
     
      struts.objectFactory = spring
  
   或者在struts.xml配置文件中进行常量的配置:
   <struts>
              <constant name="struts.objectFactory" value="spring" /> 
   </struts>
    这样,Struts2+spring就已经整合好了,下面可以写的测试类来测试搭建的环境是不是已经OK;

    类,相当于Action,只是Struts2的Action可以是普通的JAVABEAN;
    public class HelloWorld{
        private String path;
        public void setPath(String path){
              this.path = path;
        }
        public String testStruts(){ 
              return this.path;
        }
    }
    在struts.xml中进行配置
       <package name="test"  extends="struts-default">
             <action name="helloWorld" class="com.sanling.test.HelloWorld" method="testStruts">
                     <result name="OK">/index.jsp</result> 
              </action>
       </package>
    在spring配置文件中配置HelloWorld,并传个值测试;
       <bean class="com.sanling.test.HelloWorld">
                <property name="path" value="OK"></property>
       </bean>
   
    OK,启动Tomcat,访问http://localhost:8080/struts2_spring/helloWorld.do

    总结:
      Struts2和Spring整合的使用,使用到Spring插件包struts2-spring-plugin-x-x-x.jar,
    这个包的同Struts2一起发布的。Spring插件是通过覆盖Struts2的ObjectFactory来增强核心
    框架对象的创建。当创建一个对象的时候,会用到Struts2配置文件中的clss属性去和Spring
    配置文件中的id属性进行关联。如果能找到,则由Spring创建,否则由Struts2框架自身创建,
    然后由Spring来装配。Spring插件具体有如下几个作用:

    1、允许Spring创建Action Interceptor和Result.
    2、由Struts创建的对象能够被Spring装配;
    3、如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配Acton.

    开发的时候,可以不需要在Spring配置文件中注册action,Struts框架会自动自动从action mapping中创建action对象。
   
    Struts2框架整合Spring后,处理用户请求的Action并不是Struts框架创建,而是由Spring
    插件创建。创建实例时,不是利用配置Action时指定的class属性值,而是根据bean的配置id属性,从Spring容器中获得相应的实例。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值