J2EE三大框架配置文件管理示例

三大框架在整合时通常都是用spring管理hibernate和struts,配置文件一般也是采用spring的配置文件,这样一个spring的配置文件的内容多而且杂,观察起来很不清楚。现在将三大框架的配置文件分开,这样看起来就很清晰。

三大框架在web.xml中的配置:

  1. < span>xmlversion="1.0"encoding="UTF-8"?<
  2. < 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"<
  3. < span>display-name<STRUTS2display-name<
  4. < span>welcome-file-list<
  5. < span>welcome-file<index.htmlwelcome-file<
  6. < span>welcome-file<index.htmwelcome-file<
  7. < span>welcome-file<index.jspwelcome-file<
  8. < span>welcome-file<default.htmlwelcome-file<
  9. < span>welcome-file<default.htmwelcome-file<
  10. < span>welcome-file<default.jspwelcome-file<
  11. welcome-file-list<


  12. < span>servlet<
  13. < span>servlet-name<InitServletservlet-name<
  14. < span>servlet-class<com.STRUTSFRAMEWORK2.common.init.InitServletservlet-class<
  15. < span>init-param<
  16. < span>param-name<log4jparam-name<
  17. < span>param-value<WEB-INF/log4j.propertiesparam-value<
  18. init-param<
  19. < span>init-param<
  20. < span>param-name<hibernateparam-name<
  21. < span>param-value<WEB-INF/hibernate.cfg.xmlparam-value<
  22. init-param<
  23. < span>load-on-startup<1load-on-startup<
  24. servlet<


  25. < span>filter<
  26. < span>filter-name<CharacterFilterfilter-name<
  27. < span>filter-class<com.STRUTSFRAMEWORK2.common.filter.character.CharacterFilterfilter-class<
  28. < span>init-param<
  29. < span>param-name<encodingparam-name<
  30. < span>param-value<UTF-8param-value<
  31. init-param<
  32. filter<
  33. < span>filter-mapping<
  34. < span>filter-name<CharacterFilterfilter-name<
  35. < span>url-pattern</*url-pattern<
  36. filter-mapping<


  37. < span>filter<
  38. < span>filter-name<struts2filter-name<
  39. < span>filter-class<org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class<
  40. filter<
  41. < span>filter-mapping<
  42. < span>filter-name<struts2filter-name<
  43. < span>url-pattern</*url-pattern<
  44. filter-mapping<


  45. < span>listener<
  46. < span>listener-class<org.springframework.web.context.ContextLoaderListenerlistener-class<
  47. listener<
  48. < span>context-param<
  49. < span>param-name<contextConfigLocationparam-name<
  50. < span>param-value<
  51. classpath:/applicationContext.xml,
  52. classpath:/com/STRUTSFRAMEWORK2/web/*/applicationContext*.xml
  53. param-value<
  54. context-param<
  55. web-app<

其中InitServlet和CharacterFilter的代码如下:

InitServlet:

  1. package com.STRUTSFRAMEWORK2.common.init;

  2. import javax.servlet.ServletConfig;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;

  5. import org.apache.log4j.PropertyConfigurator;

  6. publicclass InitServlet extends HttpServlet {

  7. /*** serialVersionUID: ***/
  8. privatestaticfinallong serialVersionUID = -6389389969009997855L;

  9. publicstaticfinal String FILE_SEPARATOR = System.getProperties()
  10. .getProperty("file.separator");

  11. privatestatic String contextPath;

  12. privatestatic String hibernatePath;

  13. privatestatic String serverConfig;

  14. privatestatic String classPath;

  15. @Override
  16. publicvoid init(ServletConfig config) throws ServletException {

  17. super.init(config);

  18. String prefix = config.getServletContext().getRealPath("/");
  19. InitServlet.contextPath = prefix;

  20. if (FILE_SEPARATOR.equals("\\")) {
  21. // 获取内容服务器配置文件的路径
  22. serverConfig = prefix + "\\WEB-INF\\config.properties";
  23. } elseif (FILE_SEPARATOR.equals("/")) {
  24. serverConfig = prefix + "/WEB-INF/config.properties";
  25. }

  26. // Log4J
  27. String log4jFile = config.getInitParameter("log4j");
  28. String log4jConfigPath = prefix + log4jFile;
  29. PropertyConfigurator.configure(log4jConfigPath);

  30. // Hibernate Path
  31. hibernatePath = prefix + config.getInitParameter("hibernate");

  32. classPath = Thread.currentThread().getContextClassLoader()
  33. .getResource("").getPath();
  34. }

  35. @Override
  36. publicvoid destroy() {
  37. super.destroy();
  38. }

  39. publicstaticfinal String getContextPath() {
  40. return InitServlet.contextPath;
  41. }

  42. publicstaticfinal String getHibernatePath() {
  43. return InitServlet.hibernatePath;
  44. }

  45. publicstaticfinal String getServerConfig() {
  46. return serverConfig;
  47. }

  48. publicstaticfinal String getClassPath() {
  49. return classPath;
  50. }

  51. }

CharacterFilter:

  1. package com.STRUTSFRAMEWORK2.common.filter.character;

  2. import java.io.IOException;

  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;

  9. publicclass CharacterFilter implements Filter {

  10. protected String encoding = null;
  11. protected FilterConfig filterConfig = null;
  12. protectedboolean ignore = true;

  13. @Override
  14. publicvoid destroy() {
  15. this.encoding = null;
  16. this.filterConfig = null;
  17. }

  18. @Override
  19. publicvoid doFilter(ServletRequest request, ServletResponse response,
  20. FilterChain chain) throws IOException, ServletException {
  21. if (ignore || (request.getCharacterEncoding() == null)) {
  22. String encoding = selectEncoding(request);
  23. if (encoding != null) {
  24. request.setCharacterEncoding(encoding);
  25. }
  26. }
  27. chain.doFilter(request, response);
  28. }

  29. @Override
  30. publicvoid init(FilterConfig filterConfig) throws ServletException {
  31. this.filterConfig = filterConfig;
  32. this.encoding = filterConfig.getInitParameter("encoding");
  33. String value = filterConfig.getInitParameter("ignore");
  34. if (value == null) {
  35. this.ignore = true;
  36. } elseif (value.equalsIgnoreCase("true")) {
  37. this.ignore = true;
  38. } elseif (value.equalsIgnoreCase("yes")) {
  39. this.ignore = true;
  40. } else {
  41. this.ignore = false;
  42. }
  43. }

  44. protected String selectEncoding(ServletRequest request) {
  45. return (this.encoding);
  46. }
  47. }


在src(源代码文件夹)下加入applicationContext.xml和struts.xml文件,这两个文件的角色是spring和struts的“总管家”

struts.xml文件:

  1. < span>xmlversion="1.0"encoding="UTF-8"?<

  2. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd"<

  4. < span>struts<

  5. < span>constantname="struts.objectFactory"value="org.apache.struts2.spring.StrutsSpringObjectFactory"/<
  6. < span>constantname="struts.multipart.maxSize"value="104857600"<constant<

  7. < span>includefile="/com/STRUTSFRAMEWORK2/web/test/struts-test.xml"<include<
  8. struts<


applicationContext.xml文件:

  1. < span>xmlversion="1.0"encoding="UTF-8"?<
  2. < span>beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-autowire="autodetect"<

  9. < span>aop:aspectj-autoproxyproxy-target-class="true"/<


  10. < span>beanid="dao"class="com.STRUTSFRAMEWORK2.common.dao.impl.CommonDaoImpl"<
  11. bean<
  12. 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:

  1. < span>xmlversion="1.0"encoding="UTF-8"?<

  2. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd"<
  4. < span>struts<
  5. < span>packagename="test"extends="struts-default"namespace="/sshframework/api"<
  6. < span>actionname="testCheck"class="com.STRUTSFRAMEWORK2.web.test.action.TestAction"<
  7. < span>resultname="success"</pages/testpages/success.jspresult<
  8. < span>resultname="error"</pages/testpages/error.jspresult<
  9. < span>resultname="fail"</pages/testpages/fail.jspresult<
  10. action<
  11. package<
  12. struts<


applicationContext_test.xml:

  1. < span>xmlversion="1.0"encoding="UTF-8"?<
  2. < span>beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"<

  9. beans<

这样的好处是每个模块的配置文件不会相互干扰,系统结构清晰,O(∩_∩)O哈哈~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值