struts2+hibernate3+spring3整合(基于xml)
一、整合struts2
1、导入struts2的相关jar包
2、引入2个jsp,person.jsp和success.jsp
3、在cn.itcast.daomin建立Person
4、在src的类路径下创建struts.xml
5、在cn.itcast.web.action中创建PersonAction
6、在web.xml中添加struts2的过滤器,这是struts2开发的核心
二、整合hibernate
1、导入相关的jar包
2、创建对应Person类的映射文件,Person.hbm.xml
3、在src下创建hibernate.cfg.xml的配置文件
三、struts2整合spring
1、导入相应struts2-spring-plugin-2.1.8.1.jar
2、在web.xml中配置
<!-- 配置上下文对象,找到beans.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 2种写法,第一种
<param-value>/WEB-INF/classes/beans.xml</param-value>
-->
<!-- 第二种 -->
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 配置监听,让web服务启动的时候,加载beans.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、将beans.xml与struts.xml紧密集合
(1)将struts.xml中:
<action name="personAction_*" class="cn.itcast.web.action.PersonAction" method="{1}">
<result name="success">
/success.jsp
</result>
</action>
改成:
<action name="personAction_*" class="personAction" method="{1}">
<result name="success">
/success.jsp
</result>
</action>
(2)在beans.xml中:
将<bean id="personAction" class="cn.itcast.web.action.PersonAction" scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
添加:scope="prototype",表示Action是实例
四、spring整合hibernate
(1)修改beans.xml,添加
<!-- spring整合hibernate -->
<!-- 1、创建sessionFactory工厂,这是spring整合hibernate的核心 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 2、创建hibernate模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- dao层 -->
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- service层 -->
<bean id="personService" class="cn.itcast.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<!-- action层 -->
<bean id="personAction" class="cn.itcast.web.action.PersonAction" scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
</beans>
(5)在beans.xml中添加事务管理器(XML)
<!-- 控制事务 -->
<!-- 配置hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用通知定义事务 -->
<tx:advice id="adviceTxManager" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 将通知关联切入点(将事务关联业务层) -->
<aop:config>
<aop:pointcut id="pointcutService" expression="execution(* cn.itcast.service..*.*(..))"/>
<aop:advisor advice-ref="adviceTxManager" pointcut-ref="pointcutService"/>
</aop:config>