Struts2技巧之集成Spring

传统业务公司就是毛病多,这次是公司要求以一个老旧的struts2项目为模板,拆解出一套新项目的框架。

1. 前提

这个行业需要学习的东西太多了,现在的项目使用struts2也是越来越少。所以一直以来也没对其底层实现和源码进行了解。所以本文只能算做一个记录,而非深层次研究性文章。

2. 引入plugin

在项目的pom.xml文件中引入如下内容:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>2.5.14.1</version>
</dependency>

这将引入一个名为struts2-spring-plugin-2.5.14.1.jar的JAR包。

注意该jar下的 struts-plugin.xml 文件,里面有一些在引入Spring作为容器之后,针对struts2自身作的一些配置修改。例如<constant name="struts.objectFactory" value="spring" />等等。这样就省去了我们自己动手的重复性工作。

3. struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!--注意这里的2.5版本号-->
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="qd" extends="struts-basic"  namespace="/qd">
    <!-- 如果有通配符, 记得启用这个
        <global-allowed-methods>regex:.*</global-allowed-methods>
     -->        
        <!--这里的 qdCoreAction 是 spring容器中Bean的Id -->
        <!-- 稍微深入一些就可以发现,SpringObjectFactory 去实现的  ObjectFactory.buildBean 方法中,可以看到其首先尝试以该名称为bean Id从容器里加载,如果发现容器里没有的话,就会按照struts2之前的逻辑。-->
        <action name="work" class="qdCoreAction" method="work"></action>                
    </package>  
</struts>

4. web.xml

<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定ContextLoaderListener将要读取的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:./config/spring-config.xml,classpath:./config/spring-struts.xml
       </param-value>
</context-param>

<!-- 依然是由struts2来接收请求 -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
            <!-- https://blog.csdn.net/itmyhome1990/article/details/39344263 -->
            <!-- struts-default.xml 位于: struts2-core-2.5.14.jar 中 -->
            <!-- struts-plugin.xml  位于: struts2-json-plugin-2.5.14.jar, struts2-convention-plugin-2.5.14.jar等 -->
            <!-- 下面这些配置文件,在前面的先解析 -->
            <!-- struts-default.xml,struts-plugin.xml,config/struts-base.xml,struts.xml, 全部放到下面这个配置文件中导入 -->
            config/struts.xml
        </param-value>
    </init-param>
</filter>

5. spring-config.xml

    <!--这里的action一定不能是单例的-->
    <bean id="qdCoreAction" class="com.kanq.ycsl.base.qd.QdCoreAction" scope="prototype">
        <!-- 引用的属性可以是单例的 -->
        <property name="visitor" ref="coreService"></property>
    </bean>

6. 注意事项

  1. 获取应用中的ApplicationContext实例
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);
  1. Spring整合Struts2的两种方式
  2. Office Site
  3. plugin-Spring
  4. struts2-plugin集合
  5. Struts2 2.5.2的套路
    1. 笔者碰到的一个坑就是 将Struts2升级到2.5版本后,折腾半天,才发现其默认是禁止使用通配符。

猜你喜欢

转载自blog.csdn.net/lqzkcx3/article/details/80597461