三大框架在整合时通常都是用spring管理hibernate和struts,配置文件一般也是采用spring的配置文件,这样一个spring的配置文件的内容多而且杂,观察起来很不清楚。现在将三大框架的配置文件分开,这样看起来就很清晰。
三大框架在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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>STRUTS2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 系统初始化 --> <servlet> <servlet-name>InitServlet</servlet-name> <servlet-class>com.STRUTSFRAMEWORK2.common.init.InitServlet</servlet-class> <init-param> <param-name>log4j</param-name> <param-value>WEB-INF/log4j.properties</param-value> </init-param> <init-param> <param-name>hibernate</param-name> <param-value>WEB-INF/hibernate.cfg.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 字符拦截器 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.STRUTSFRAMEWORK2.common.filter.character.CharacterFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 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> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/applicationContext.xml, classpath:/com/STRUTSFRAMEWORK2/web/*/applicationContext*.xml </param-value> </context-param> </web-app>
其中InitServlet和CharacterFilter的代码如下:
InitServlet:
package com.STRUTSFRAMEWORK2.common.init; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.PropertyConfigurator; public class InitServlet extends HttpServlet { /*** serialVersionUID: ***/ private static final long serialVersionUID = -6389389969009997855L; public static final String FILE_SEPARATOR = System.getProperties() .getProperty("file.separator"); private static String contextPath; private static String hibernatePath; private static String serverConfig; private static String classPath; @Override public void init(ServletConfig config) throws ServletException { super.init(config); String prefix = config.getServletContext().getRealPath("/"); InitServlet.contextPath = prefix; if (FILE_SEPARATOR.equals("\\")) { // 获取内容服务器配置文件的路径 serverConfig = prefix + "\\WEB-INF\\config.properties"; } else if (FILE_SEPARATOR.equals("/")) { serverConfig = prefix + "/WEB-INF/config.properties"; } // Log4J String log4jFile = config.getInitParameter("log4j"); String log4jConfigPath = prefix + log4jFile; PropertyConfigurator.configure(log4jConfigPath); // Hibernate Path hibernatePath = prefix + config.getInitParameter("hibernate"); classPath = Thread.currentThread().getContextClassLoader() .getResource("").getPath(); } @Override public void destroy() { super.destroy(); } public static final String getContextPath() { return InitServlet.contextPath; } public static final String getHibernatePath() { return InitServlet.hibernatePath; } public static final String getServerConfig() { return serverConfig; } public static final String getClassPath() { return classPath; } }
CharacterFilter:
package com.STRUTSFRAMEWORK2.common.filter.character; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class CharacterFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; @Override public void destroy() { this.encoding = null; this.filterConfig = null; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); } } chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) { this.ignore = true; } else if (value.equalsIgnoreCase("true")) { this.ignore = true; } else if (value.equalsIgnoreCase("yes")) { this.ignore = true; } else { this.ignore = false; } } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }
在src(源代码文件夹)下加入applicationContext.xml和struts.xml文件,这两个文件的角色是spring和struts的“总管家”
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <constant name="struts.multipart.maxSize" value="104857600"></constant> <!-- 示例 --> <include file="/com/STRUTSFRAMEWORK2/web/test/struts-test.xml"></include> </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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-autowire="autodetect"> <!-- 上面的"default-autowire"和下面的"aop:aspectj-autoproxy"必不可少 --> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 示例 --> <bean id="dao" class="com.STRUTSFRAMEWORK2.common.dao.impl.CommonDaoImpl"> </bean> </beans>
在web.xml文件中已经指定了spring的配置文件在classpath下的applicationContext.xml和com.STRUTSFRAMEWORK2.web包下(包括所有的子包),spring会自动搜索。
贴上com.STRUTSFRAMEWORK2.web.test下(“test模块”的“管理文件”)struts-test.xml(在总管家注册),applicationContext_test.xml(spring自动搜索)
struts-test.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="test" extends="struts-default" namespace="/sshframework/api"> <action name="testCheck" class="com.STRUTSFRAMEWORK2.web.test.action.TestAction"> <result name="success">/pages/testpages/success.jsp</result> <result name="error">/pages/testpages/error.jsp</result> <result name="fail">/pages/testpages/fail.jsp</result> </action> </package> </struts>
applicationContext_test.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 使用spring管理 --> </beans>
这样的好处是每个模块的配置文件不会相互干扰,系统结构清晰,O(∩_∩)O哈哈~