基于Struts 2.x + Spring 2.x + Hibernate 3.x整合开发
添加Jar包:
1、添加Spring的Jar包
2、添加Hibernate的Jar包
3、添加Struts的jar包
需要添加一个基于Struts2.x整合Spring2.x的一个插件包:
struts2-spring-plugin-2.1.8.1.jar
一、基于Spring与Hibernate整合(Spring 2.x + Hibernate 3.x)
1、配置数据源:
<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/test" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="10" />
</bean>
2、配置SessionFactory
说明:
不使用hibernate.cfg.xml配置文件
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>pojo/User.hbm.xml</value>
</list>
</property>
</bean>
使用hibernate.cfg.xml配置文件,在类路径下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
3、配置事务管理器:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
4、配置事务的传播特性
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" />
</tx:attributes>
-->
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
5、配置参与事务的方法
<aop:config>
<aop:pointcut id="txMethods" expression="execution(* 包名..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethods" />
</aop:config>
二、基于Struts2与Spring整合(Struts 2.x + Spring 2.x)
向应用程序中添加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>
<!-- 配置Struts的编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定Action实例由Spring提供 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 指定Action的后缀名称为.do -->
<constant name="struts.action.extension" value="do" />
<!-- 指定浏览器不缓存 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 关闭动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 指定Struts的模式为开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 指定视图模式为简单模式 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 设置指定修改struts.xml文件时自动重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<package name="base" extends="struts-default" abstract="true" />
</struts>
1、向应用程序作用域添加Spring配置文件,多个文件用逗号分隔
在类路径中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
在WEB-INF目录下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
以上两种情况需要添加监听器:
向WEB应用添加Spring提供的监听器,用来实例化Spring容器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、第一种方案:
采用依赖查找:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
第二种:依赖注入:
<!-- 指定Action实例由Spring提供 -->
<constant name="struts.objectFactory" value="spring" />
在指定的Action的class属性中指定为Spring中提供的id属性
<!-- 向WEB应用添加参数,用于读取Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 用来读取Spring配置文件,实例化Spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 清理ActionContext数据 -->
<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>
<!-- 让Session一直开启,直到发送生成响应之前 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</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>