J2EE三大框架配置文件管理示例
2013-10-07 14:43
656 查看
三大框架在整合时通常都是用spring管理hibernate和struts,配置文件一般也是采用spring的配置文件,这样一个spring的配置文件的内容多而且杂,观察起来很不清楚。现在将三大框架的配置文件分开,这样看起来就很清晰。
三大框架在web.xml中的配置:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>web-appxmlns: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"<
< span>display-name<STRUTS2display-name<
< span>welcome-file-list<
< span>welcome-file<index.htmlwelcome-file<
< span>welcome-file<index.htmwelcome-file<
< span>welcome-file<index.jspwelcome-file<
< span>welcome-file<default.htmlwelcome-file<
< span>welcome-file<default.htmwelcome-file<
< span>welcome-file<default.jspwelcome-file<
welcome-file-list<
< span>servlet<
< span>servlet-name<InitServletservlet-name<
< span>servlet-class<com.STRUTSFRAMEWORK2.common.init.InitServletservlet-class<
< span>init-param<
< span>param-name<log4jparam-name<
< span>param-value<WEB-INF/log4j.propertiesparam-value<
init-param<
< span>init-param<
< span>param-name<hibernateparam-name<
< span>param-value<WEB-INF/hibernate.cfg.xmlparam-value<
init-param<
< span>load-on-startup<1load-on-startup<
servlet<
< span>filter<
< span>filter-name<CharacterFilterfilter-name<
< span>filter-class<com.STRUTSFRAMEWORK2.common.filter.character.CharacterFilterfilter-class<
< span>init-param<
< span>param-name<encodingparam-name<
< span>param-value<UTF-8param-value<
init-param<
filter<
< span>filter-mapping<
< span>filter-name<CharacterFilterfilter-name<
< span>url-pattern</*url-pattern<
filter-mapping<
< span>filter<
< span>filter-name<struts2filter-name<
< span>filter-class<org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class<
filter<
< span>filter-mapping<
< span>filter-name<struts2filter-name<
< span>url-pattern</*url-pattern<
filter-mapping<
< span>listener<
< span>listener-class<org.springframework.web.context.ContextLoaderListenerlistener-class<
listener<
< span>context-param<
< span>param-name<contextConfigLocationparam-name<
< span>param-value<
classpath:/applicationContext.xml,
classpath:/com/STRUTSFRAMEWORK2/web/*/applicationContext*.xml
param-value<
context-param<
web-app<
其中InitServlet和CharacterFilter的代码如下:
InitServlet:
[java]view
plaincopyprint?
package
com.STRUTSFRAMEWORK2.common.init;
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
org.apache.log4j.PropertyConfigurator;
publicclass
InitServlet extends HttpServlet {
/*** serialVersionUID: ***/
privatestaticfinallong
serialVersionUID = -6389389969009997855L;
publicstaticfinal
String FILE_SEPARATOR = System.getProperties()
.getProperty("file.separator");
privatestatic
String contextPath;
privatestatic
String hibernatePath;
privatestatic
String serverConfig;
privatestatic
String classPath;
@Override
publicvoid
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";
}
elseif
(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
publicvoid
destroy() {
super.destroy();
}
publicstaticfinal
String getContextPath() {
return
InitServlet.contextPath;
}
publicstaticfinal
String getHibernatePath() {
return
InitServlet.hibernatePath;
}
publicstaticfinal
String getServerConfig() {
return
serverConfig;
}
publicstaticfinal
String getClassPath() {
return
classPath;
}
}
CharacterFilter:
[java]view
plaincopyprint?
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;
publicclass
CharacterFilter implements Filter {
protected
String encoding = null;
protected
FilterConfig filterConfig = null;
protectedboolean
ignore = true;
@Override
publicvoid
destroy() {
this.encoding =
null;
this.filterConfig =
null;
}
@Override
publicvoid
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
publicvoid
init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if
(value == null) {
this.ignore =
true;
}
elseif
(value.equalsIgnoreCase("true")) {
this.ignore =
true;
}
elseif
(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文件:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"<
< span>struts<
< span>constantname="struts.objectFactory"value="org.apache.struts2.spring.StrutsSpringObjectFactory"/<
< span>constantname="struts.multipart.maxSize"value="104857600"<constant<
< span>includefile="/com/STRUTSFRAMEWORK2/web/test/struts-test.xml"<include<
struts<
applicationContext.xml文件:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>beansxmlns="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"<
< span>aop:aspectj-autoproxyproxy-target-class="true"/<
< span>beanid="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:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"<
< span>struts<
< span>packagename="test"extends="struts-default"namespace="/sshframework/api"<
< span>actionname="testCheck"class="com.STRUTSFRAMEWORK2.web.test.action.TestAction"<
< span>resultname="success"</pages/testpages/success.jspresult<
< span>resultname="error"</pages/testpages/error.jspresult<
< span>resultname="fail"</pages/testpages/fail.jspresult<
action<
package<
struts<
applicationContext_test.xml:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>beansxmlns="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"<
beans<
这样的好处是每个模块的配置文件不会相互干扰,系统结构清晰,O(∩_∩)O哈哈~
三大框架在web.xml中的配置:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>web-appxmlns: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"<
< span>display-name<STRUTS2display-name<
< span>welcome-file-list<
< span>welcome-file<index.htmlwelcome-file<
< span>welcome-file<index.htmwelcome-file<
< span>welcome-file<index.jspwelcome-file<
< span>welcome-file<default.htmlwelcome-file<
< span>welcome-file<default.htmwelcome-file<
< span>welcome-file<default.jspwelcome-file<
welcome-file-list<
< span>servlet<
< span>servlet-name<InitServletservlet-name<
< span>servlet-class<com.STRUTSFRAMEWORK2.common.init.InitServletservlet-class<
< span>init-param<
< span>param-name<log4jparam-name<
< span>param-value<WEB-INF/log4j.propertiesparam-value<
init-param<
< span>init-param<
< span>param-name<hibernateparam-name<
< span>param-value<WEB-INF/hibernate.cfg.xmlparam-value<
init-param<
< span>load-on-startup<1load-on-startup<
servlet<
< span>filter<
< span>filter-name<CharacterFilterfilter-name<
< span>filter-class<com.STRUTSFRAMEWORK2.common.filter.character.CharacterFilterfilter-class<
< span>init-param<
< span>param-name<encodingparam-name<
< span>param-value<UTF-8param-value<
init-param<
filter<
< span>filter-mapping<
< span>filter-name<CharacterFilterfilter-name<
< span>url-pattern</*url-pattern<
filter-mapping<
< span>filter<
< span>filter-name<struts2filter-name<
< span>filter-class<org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class<
filter<
< span>filter-mapping<
< span>filter-name<struts2filter-name<
< span>url-pattern</*url-pattern<
filter-mapping<
< span>listener<
< span>listener-class<org.springframework.web.context.ContextLoaderListenerlistener-class<
listener<
< span>context-param<
< span>param-name<contextConfigLocationparam-name<
< span>param-value<
classpath:/applicationContext.xml,
classpath:/com/STRUTSFRAMEWORK2/web/*/applicationContext*.xml
param-value<
context-param<
web-app<
其中InitServlet和CharacterFilter的代码如下:
InitServlet:
[java]view
plaincopyprint?
package
com.STRUTSFRAMEWORK2.common.init;
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
org.apache.log4j.PropertyConfigurator;
publicclass
InitServlet extends HttpServlet {
/*** serialVersionUID: ***/
privatestaticfinallong
serialVersionUID = -6389389969009997855L;
publicstaticfinal
String FILE_SEPARATOR = System.getProperties()
.getProperty("file.separator");
privatestatic
String contextPath;
privatestatic
String hibernatePath;
privatestatic
String serverConfig;
privatestatic
String classPath;
@Override
publicvoid
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";
}
elseif
(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
publicvoid
destroy() {
super.destroy();
}
publicstaticfinal
String getContextPath() {
return
InitServlet.contextPath;
}
publicstaticfinal
String getHibernatePath() {
return
InitServlet.hibernatePath;
}
publicstaticfinal
String getServerConfig() {
return
serverConfig;
}
publicstaticfinal
String getClassPath() {
return
classPath;
}
}
CharacterFilter:
[java]view
plaincopyprint?
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;
publicclass
CharacterFilter implements Filter {
protected
String encoding = null;
protected
FilterConfig filterConfig = null;
protectedboolean
ignore = true;
@Override
publicvoid
destroy() {
this.encoding =
null;
this.filterConfig =
null;
}
@Override
publicvoid
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
publicvoid
init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if
(value == null) {
this.ignore =
true;
}
elseif
(value.equalsIgnoreCase("true")) {
this.ignore =
true;
}
elseif
(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文件:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"<
< span>struts<
< span>constantname="struts.objectFactory"value="org.apache.struts2.spring.StrutsSpringObjectFactory"/<
< span>constantname="struts.multipart.maxSize"value="104857600"<constant<
< span>includefile="/com/STRUTSFRAMEWORK2/web/test/struts-test.xml"<include<
struts<
applicationContext.xml文件:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>beansxmlns="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"<
< span>aop:aspectj-autoproxyproxy-target-class="true"/<
< span>beanid="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:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"<
< span>struts<
< span>packagename="test"extends="struts-default"namespace="/sshframework/api"<
< span>actionname="testCheck"class="com.STRUTSFRAMEWORK2.web.test.action.TestAction"<
< span>resultname="success"</pages/testpages/success.jspresult<
< span>resultname="error"</pages/testpages/error.jspresult<
< span>resultname="fail"</pages/testpages/fail.jspresult<
action<
package<
struts<
applicationContext_test.xml:
[html]view
plaincopyprint?
< span>xmlversion="1.0"encoding="UTF-8"?<
< span>beansxmlns="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"<
beans<
这样的好处是每个模块的配置文件不会相互干扰,系统结构清晰,O(∩_∩)O哈哈~
相关文章推荐
- 【追加】J2EE三大框架配置文件管理示例--hibernate配置文件示例
- J2EE三大框架配置文件管理示例
- 【追加】J2EE三大框架配置文件管理示例--文件结构和所需的jar包
- 【追加】J2EE三大框架配置文件管理示例--加入hibernate
- Java三大框架---Struts2--(分文件管理配置文件和数据封装)
- 服务器集群管理框架从构思到完成(三)高级应用示例:修改远程windows和linux服务器配置文件
- 最新的三大框架整合之配置文件详解
- Zend框架 ------ 管理配置文件
- 三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板
- ssh三大框架整合配置文件及测试代码
- 三大框架学习1——struts2的两个核心配置文件
- Java三大框架之struts的上传文件出错信息配置(允许上传的类型,上传文件的大小,以及大文件上传说明等等)
- 【开源.NET】轻量级内容管理框架Grissom.CMS(第三篇解析配置文件和数据以转换成 sql)
- 黑马程序员--06.管理配置文件--【框架和工具】【配置文件的管理】
- 三大框架的配置文件(SSM)
- .NET 插件系统框架设计(二) 使用对象序列化实现自定义配置文件管理
- Spring+mybatis 框架(配置文件、代码示例)
- Spring ,Springmvc,mybatis三大框架整合的配置文件支持
- Maven 管理SSM框架的pom.xml配置文件
- SSH三大框架整合使用的配置文件 注解实现