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

 三大框架在整合时通常都是用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哈哈~
 

 

 

 

 

 

JEECMS是JavaEE版网站管理系统(Java Enterprise Edition Content Manage System)的简称。 基于java技术开发,继承其强、稳定、安全、高效、跨平台等多方面的优点 采用hibernate3+struts2+spring2+freemarker主流技术架构 懂html就能建站,提供最便利、合理的使用方式 强、灵活的标签,用户自定义显示内容和显示方式 在设计上自身预先做了搜索引擎优化,增强JEECMS v2.3.2 正式版具有如下特性: 一、系统内核完全开源,为用户提供透明的管理平台,为java初学者提供良好的学习平台; 二、核心模板全部使用文件形式保存,自由导入/导出模板,极方便了用户模板设计、网站升级转移、全站换肤; 、首页、栏目页缓存处理,在启动了页面缓存后,有利于提高系统反应速度,降低系统资源的消耗; 四、用户自定义添加和切换分页、焦点图样式; 五、轻松建立并管理多站点,所有站点权限和内容独立管理,站点间实现单点登录; 五、面向未来的过渡,JEECMS v2.3.2 Final版是JEECMS面世以来发布的第一个正式版本,具有更的灵活性和稳定性; JEECMS v2.4新版功能介绍: 1、专题 (应网友要求开发) 2、全文检索 (应网友要求开发) 3、强化自定义标签功能(应网友要求开发) 4、下载频道 5、自动生成子站点、模板、栏目 6、删除子站点 7、整站(主站与子站点)搜索 以下是JEECMS v2.4.1 beta版更新的详细内容: 1、修正了后台用户密码修改的问题 2、修正了全文检索报错的问题 3、修正了全文检索生成索引时tomcat报错的问题 4、修正了下载内容无法修改的问题 5、修正了发布文章、修改文章出错的问题 6、修正了建立子站报错的问题 7、修正了文章评论设置成需要登录才能评论时无效的问题 8、修正了下载详细内容页中“浏览次数”、“下载次数”不更新的问题 9、修正了文章评论修正时无法审核的问题 10、修正了文章评论表单中的图片显示问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值