1.引入所需要的jar包
2.配置web.xml,增加struts2的所需要的过滤器配置。
3. 修改web.xml文件,配置Spring监听器,和上下文变量。
4. 编写spring配置文件,配置管理hibernate的sessionFactory和事务管理、datasource等,以及actionBean
5. 配置hibernate配置文件,如果使用annotation,则配置entity
6. 配置struts2配置文件
2.配置web.xml,增加struts2的所需要的过滤器配置。
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/resources/conf/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. 修改web.xml文件,配置Spring监听器,和上下文变量。
<!-- 1、指定spring的配置文件,默认从web根目录寻找配置文件, 我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/resources/conf/appContext*.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4. 编写spring配置文件,配置管理hibernate的sessionFactory和事务管理、datasource等,以及actionBean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 配置数据库信息 -->
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/rabbit?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="rabbit" />
<property name="password" value="19870924" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<!-- 通过配置文件配置sessionFactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>resources/conf/hibernate.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>-->
<!-- 通过annotationSessionFactoryBean使用annotation-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<value>com.**.entity.**</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<!-- 事务控制 -->
<bean id="transactionController"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用拦截器进行事务控制 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionController" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED </prop>
<prop key="add*">PROPAGATION_REQUIRED </prop>
<prop key="execute*">PROPAGATION_REQUIRED </prop>
<prop key="newTran*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<!-- 自动代理 -->
<bean id="autoproxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Manager</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="baseDao" class="com.base.dao.impl.BaseDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="baseManager" class="com.base.manager.impl.BaseManagerImpl">
<property name="dao" ref="baseDao"></property>
</bean>
</beans>
5. 配置hibernate配置文件,如果使用annotation,则配置entity
6. 配置struts2配置文件
<?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.devMode" value="true"></constant>
<!-- 修改默认的后缀 -->
<constant name="struts.action.extension" value="do,action"></constant>
<!-- 与spring的整合 -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="common" abstract="true" extends="struts-default">
配置action
</package>
</struts>