【引言】 本来spring就有mvc,可是大家用struts习惯了,最近用spring来管理后台dao,struts2来响应请求。面对一大堆的配置文件,classes路径下一堆, /WEB-INF/下一堆,很分散,看着也不爽。于是就想把他们放在一起。
1、配置文件及位置分布:
log4j.properties src目录下
spring相关配置文件: src/config/spring/*.xml
applicationContext.xml src/config/spring/applicationContext.xml applicationContext-action.xml src/config/spring/applicationContext-action.xml applicationContext-dao.xml src/config/spring/applicationContext-dao.xml applicationContext-service.xml src/config/spring/applicationContext-service.xml
struts相关配置文件: src/config/struts/*.xml
struts.xml src/config/struts/struts.xml struts_query.xml src/config/struts/struts_query.xml struts_common.xml src/config/struts/struts_common.xml
2.web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 用于初始化Spring的Listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring的log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Spring核心配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/spring/applicationContext.xml, classpath:config/spring/applicationContext-*.xml </param-value> </context-param> <filter> <filter-name>struts2-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class> <init-param> <param-name>config</param-name> <!-- config files for struts2, struts-default.xml, struts-plugin.xml can't delete --> <param-value> struts-default.xml, struts-plugin.xml, /config/struts/struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
说明:
1、要是struts.xml文件不放在默认的src文件夹下,需要增加struts-default.xml, struts-plugin.xml两个配置文件。然后在需要配置的Filter下增加以下参数:
<init-param> <param-name>config</param-name> <!-- config files for struts2, struts-default.xml, struts-plugin.xml can't delete --> <param-value> struts-default.xml, struts-plugin.xml, /config/struts/struts.xml </param-value> </init-param>
2、没有使用 ActionContextCleanUp Filter的话,直接配置 PrepareAndExecuteFilter就ok了。
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <!-- config files for struts2, struts-default.xml, struts-plugin.xml can't delete --> <param-value> struts-default.xml, struts-plugin.xml, /config/struts/struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
若已经使用了 ActionContextCleanUp Filter的话,需同时配置两个如下Filter。
<filter> <filter-name>struts2-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class> <init-param> <param-name>config</param-name> <!-- config files for struts2, struts-default.xml, struts-plugin.xml can't delete --> <param-value> struts-default.xml, struts-plugin.xml, /config/struts/struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping><filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>