簡介:html
- 由Spring建立並管理Action
- 使用Spring的IOC功能將業務類注入Action
- 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
<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 著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。