Spring整合Struts2

简介:html

  1. 由Spring建立并管理Action
  2. 使用Spring的IOC功能将业务类注入Action
  3. Spring容器经过Web容器启动(配置监听器ContextLoaderListener便可完成)

配置


1.导入依赖包java

​ 除Struts2和Spring框架自己所需的JAR文件外(这里用的是struts-2.3.28-all & spring-framework-4.2.5.RELEASE-dist),还要导入struts2-spring-plugin-x.xx.jar。web

2.web.xml配置spring

  • Struts2 核心拦截器配置:
<filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>

配置监听器ContextLoaderListenerapache

Spring提供一个ContextLoaderListener对象,该类能够做为Web应用的Listener使用,它会在Web应用启动时自动查找WEB-INF/下的applicationContext.xml配置文件(Spring的配置文件),而且根据该文件来建立Spring容器.所以,若是Web应用中只有一个Spring配置文件,而且文件名为"applicationContext.xml",并将该文件放在Web应用的WEB-INF/路径下,则只需在web.xml文件中增长以下一段便可:app

<!-- 根据默认配置文件来初始化Spring容器 -->
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

​ 若是有多个配置文件须要载入,或则applicationContext.xml不在WEB-INF目录下,则应该在web.xml中再使用<context-param>元素来肯定配置文件的文件名,内容以下:框架

<!-- 配置spring的用于初始化容器对象的监听器-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
         classpath:applicationContext*.xml,/WEB-INF/applicationContext.xml
   </param-value>
</context-param>

若是spring配置文件被命名为 applicationContext.xml,而且放在WEB-INF目录下,则不须要配置<context-param>,由于ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件。若存在多个Spring配置文件,则在<param-value>中依次列出,之间以逗号隔开。jsp

最终web.xml配置为:测试

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test</display-name>
<!-- Struts2 核心拦截器配置-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
<!-- 根据默认配置文件来初始化Spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
<!-- 配置spring的用于初始化容器对象的监听器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml,/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
</web-app>

整合测试

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- 配置为开发模式,一旦文件改动,不须要重启,会当即生效 -->
    <constant name="struts.devMode" value="true" />
    <!-- 把扩展名设置为action -->
    <constant name="struts.action.extension" value="action" />
    <!-- 把主题配置为simple -->
    <constant name="struts.ui.theme" value="simple" />
   <package name="default" namespace="/" extends="struts-default">

   <!-- 整合框架后class能够直接写bean名称 -->
   <action name="test" class="testAction">
        <result name="success">/index.jsp</result>
    </action>

    </package>
</struts>
<!-- applicationContext.xml  -->
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<!--  自动扫描与装配bean-->
<context:component-scan base-package="test"></context:component-scan>

</beans>
<!-- web.xml  -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2SpringZH</display-name>
   <!-- Strus2核心配置  -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 <!-- 根据默认配置文件来初始化Spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
<!-- 配置spring的用于初始化容器对象的监听器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml,/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
</web-app>

报错的话将<!-- web.xml -->一行删掉ui

//TestAction.java

package test;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{


    public String execute() throws Exception {
    System.out.println("TestAction.execute()");
    return "success";
    }
}
//SpringTest.java

package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.TestExecutionListeners;

public class SpringTest {
    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    //Struts整合测试
    public void testBean() throws Exception{
        TestAction testAction = (TestAction) ac.getBean("testAction");
        System.out.println(testAction);
    }
}

运行结果:(出现test.TestAction....说明整合成功)

文/吾君(简书做者) 原文连接:http://www.jianshu.com/p/7055fa640351 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。

相关文章
相关标签/搜索