Struts 2.5版本依赖备忘及spring整合注意点

版权声明:好笔头不如烂记性 https://blog.csdn.net/zsbgood/article/details/81069069

在使用Struts 2.5版本时,遇到两个问题:
1. web.xml中配置的 SturtsPrepareAndExecuteFilter 时直接报错
2. 启动tomcat时,一直提示filter启动不了

回答:
1. 报错是因为,新版本中已经没有了ng包,因此filter-class中填写
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
2. 一直提示启动filter是因为jar包版本问题,因此需要记录可用的依赖版本以备以后使用

依赖

建议到struts2的官网找到jar包组合,下载后,查看jar包版本信息再引入依赖即可。https://struts.apache.org/download.cgi

    <!--struts2============================================================================-->
    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.16</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ognl/ognl -->
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.1.15</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.23.0-GA</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.26-incubating</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm-tree -->
    <dependency>
      <groupId>org.ow2.asm</groupId>
      <artifactId>asm-tree</artifactId>
      <version>5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm-commons -->
    <dependency>
      <groupId>org.ow2.asm</groupId>
      <artifactId>asm-commons</artifactId>
      <version>5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
    <dependency>
      <groupId>org.ow2.asm</groupId>
      <artifactId>asm</artifactId>
      <version>5.2</version>
    </dependency>

    <!-- 整合spring需要的依赖-->
    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
      <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-spring-plugin</artifactId>
        <version>2.5.16</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.7.RELEASE</version>
      </dependency>

Struts2与spring整合注意点

1. web.xml引入spring

    <!--配置listener启动spring-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--指定spring配置文件位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

2. Action相关bean的scope属性设置为(prototype)原型模式,因为spring的bean默认为singleton单例

3. struts.xml中的action的class不再需要写全类名,只需要写bean的名称。

展开阅读全文

没有更多推荐了,返回首页