`
Clayz
  • 浏览: 293637 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

Struts2,Spring2,Hibernate3整合

阅读更多

1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/*.xml</param-value>
	</context-param>

	<!-- struts2本质上是一个filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</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>

	<!-- 在freemarker中使用struts2标签 -->
	<servlet>
		<servlet-name>JspSupportServlet</servlet-name>
		<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>

 

2. struts.xml

<struts>
	<constant name="struts.custom.i18n.resources" value="messageResource" />
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.ui.theme" value="simple" />
	<constant name="struts.multipart.maxSize" value="1000000000" />
	<include file="struts/demo-default.xml" />
</struts>

 

3. demo-default.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="demo-default" extends="struts-default" namespace="/">
		<!-- 使用freemarker只为页面模板 -->
		<result-types>
			<result-type name="freemarke" default="true"
				class="org.apache.struts2.views.freemarker.FreemarkerResult" />
		</result-types>

		<!-- 自定义权限拦截器 -->
		<interceptors>
			<interceptor name="authority" class="org.demo.common.AuthorityInterceptor" />
			<interceptor-stack name="demoDefaultStack">
				<interceptor-ref name="authority" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="demoDefaultStack" />

		<global-results>
			<result name="executeResult">/common/execute_result.ftl</result>
			<result name="exception">/common/exception.ftl</result>
		</global-results>

		<global-exception-mappings>
			<exception-mapping result="exception" exception="org.cms.common.ExceptionHandle" />
		</global-exception-mappings>
	</package>

	<include file="struts/demo-home.xml" />
</struts>

 

4. demo-home.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="demo-home" extends="demo-default" namespace="/home">
		<action name="index" class="org.demo.action.home.UserAction"
			method="index">
			<result name="index">homepage.ftl</result>
		</action>
	</package>
</struts>

 

5. applicationContext.xml - Spring

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 定义数据源bean,使用Apache DBCP数据源实现 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
		<!-- 可以同时闲职在连接池中的连接的最大数目 -->
		<property name="maxIdle" value="10" />
		<!-- 最大激活连接数 -->
		<property name="maxActive" value="100" />
		<!-- 请求连接最常等待时间 -->
		<property name="maxWait" value="30" />
		<!-- 是否回收被遗弃的数据库连接到连接池中 -->
		<property name="removeAbandoned" value="true" />
	</bean>

	<!--定义了Hibernate的SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 为SessionFactory注入DataSource实例 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 指定Hibernate的实体映射文件 -->
		<property name="mappingResources">
			<list>
				<value>classpath:org/demo/domain/</value>
			</list>
		</property>

		<!-- 指定Hibernate连接的系列属性 -->
		<property name="hibernateProperties">
			<props>
				<!-- 指定Hibernate连接数据库的连接方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 是否显示Hibernate产生的SQL语句 -->
				<prop key="show_sql">true</prop>
				<!-- 启动应用时,是否根据hbm文件创建数据表 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 指定Hibernate持久化操作的批操作大小 -->
				<prop key="hibernate.jdbc.batch_size">20</prop>
			</props>
		</property>
	</bean>

	<!-- 配置事务管理器,使用Hibernate局部事务管理器策略 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 配置事务拦截器 -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<!-- 下面定义事务传播属性 -->
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>

	<!-- 定义BeanNameAutoProxyCreator -->
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
		<property name="beanNames">
			<!-- 下面是所有需要自动创建事务代理的bean,此处可增加其他需要自动创建事务代理的bean -->
			<list>
				<value>demoService</value>
			</list>
		</property>
		<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->
		<property name="interceptorNames">
			<list>
				<!-- 此处可增加其他新的Interceptor -->
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>

	<!-- 定义MailSender Bean,用于发送邮件 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.timeout">25000</prop>
			</props>
		</property>
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>

	<!-- 配置MailMessage Bean -->
	<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="from" value="username@163.com" />
		<property name="subject" value="Demo" />
	</bean>
</beans

 

6. applicationContext-dao.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="daoTemplate" abstract="true">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="demoDao" class="org.demo.dao.impl.DemoDaoImpl" parent="daoTemplate" />

	<!-- Dao工厂类 -->
	<bean id="daoManager" class="org.demo.dao.DaoManager">
		<property name="demoDao">
			<ref bean="demoDao" />
		</property>
	</bean>
	
</beans>

 

7. applicationContext-service.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="demoService" class="org.demo.service.impl.DemoServiceImpl" />
	
	<bean id="serviceManager" class="org.demo.service.ServiceManager">
		<property name="demoService">
			<ref bean="demoService" />
		</property>
	</bean>
	
</beans>

 

8. freemarker.properties

default_encoding=UTF-8
number_format=#
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
auto_import=/common/page_macro.ftl as p

 

9. log4j.properties

log4j.rootLogger=INFO, CONSOLE, FILE

log4j.addivity.org.apache=true
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %c %x - %m%n

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=webapps/demo/WEB-INF/logs/file.log
log4j.appender.FILE.DatePattern=.yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[DEMO] %d - %c -%-4r [%t] %-5p %c %x - %m%n 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics