Struts1.x + Spring2.x+ Hibernate3.x 系统集成

本文介绍SSH(Struts+Spring+Hibernate)框架集成的具体步骤与配置细节,包括如何利用Spring管理Struts和Hibernate,实现事务管理及AOP等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SSH系统集成笔记:
集成目标:
1.SPRING 作为IOC容器管理struts的Action,hibernate的DAO、Session。
2.使用SPRING的AOP概念管理hibernate事务。
3.使用SPRING与其他系统框架集成组件。

步骤:
1.引入Spring类库
2.引入hibernate类库
3.引入struts类库
注意: 在引入以上类库时候,可以会出现类库包冲突的情况,以保持最新包为原则。
4.加入spring的配置文件, applicationContext.xml
5.加入hibernate配置文件,hibernate.cfg.xml
6.加入struts配置文件,struts-config.xml
7.集成hibernate事务管理,配置sessionfactory为org.springframework.orm.hibernate3.LocalSessionFactory,配置事务管理器为org.springframework.orm.hibernate3.HibernateTransactionManager,配置事务的传播特性,使用<tx:advice>元素,通过<tx:attributes>配置具体方法名的事务传播特性。使用spring的AOP作用与具体目标对象上<aop:advisor>;
8.将DAO对象集成org.springframework.orm.hibernate3.support.HibernateDAOSupport.通过HibernateTemplate实现数据测CRUD。
9.将hibernate的DAO对象注入到业务逻辑层,将业务逻辑层对象配置进IOC容器。
至此:hibernate和spring已经集成完毕。需要注意,管理hibernate事务的sessionFactory和DAO实现的SessionFactory必须为同一SessionFactory。
10.配置struts。
配置web.xml,配置ActionServlet拦截所有strtus请求。
新建struts的资源文件 resourceApplication.properties。
配置struts-config.xml文件,将资源文件加入到struts-config.xml中。
11.集成struts的Action配置到Spring的IOC容器。
将所有的Action配置到spring的IOC配置文件中,通过name唯一标识,而不使用ID,name元素的内容与struts的action配置节的path一致。
将struts配置文件中action节中type属性统一修改成org.springframework.web.struts.DelegatingActionProxy,通过其调用spring的beanfacotry创建action对象并将请求转发给目标aciton。
将org.springframework.web.context.ContxtLoaderListener监听器配置到web.xml中,并配置contextConfigLocation 参数,用于注入所有的配置文件路径。

12.配置spring对各个框架的集成。
配置字符处理类。
陪孩Hibernate的session管理类。

13.其他:
引入对应数据库的链接包。
加入log4j.xml配置文件。

配置例子:
web.xml中:

=====================================================
applicaton-commons.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<!-- 配置事务 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 配置Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="managerAdvice">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="lenderBook" propagation="REQUIRED" />
<tx:method name="returnBook" propagation="REQUIRED" />
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置spring AOP进行事务拦截处理 -->
<aop:config>
<!--
<aop:pointcut id="managerPointcut" expression="execution(* com.ebook.manager.*.*(..))"/>
-->
<!-- -->
<aop:pointcut id="managerPointcut" expression="execution(* com.ebook.dao.CommonDAOSupport.*(..))"/>


<aop:advisor id="managerAdvisor" pointcut-ref="managerPointcut" advice-ref="managerAdvice"/>
</aop:config>
</beans>
==========================================================
application-beans.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<!-- 由于DAO继承了HiberaneDAOSupport,所以需要注入SessionFactory对象,此SessionFactory就为ApplicationContext-commons.xml中的sessionFactory-->
<bean id="bookDAO" class="com.ebook.dao.BookDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="cardDAO" class="com.ebook.dao.LenderCardDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="lenderRecordDAO" class="com.ebook.dao.LenderRecordDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="returnRecordDAO" class="com.ebook.dao.ReturnRecordDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置struts的Action ,注意用name属性唯一标识,注入业务逻辑对象。-->
<bean name="/login" class="com.demo.action.UserAction" >
<property name="usermanager" ref="usermanager" />
</bean>
</beans>


=================================================================
hibernate.cfg.xml 此配置可以加入到spring的配置文件中。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:1433/ebook</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl"> true</property>
<mapping resource="com/ebook/dao/Book.hbm.xml" />
<mapping resource="com/ebook/dao/LenderCard.hbm.xml" />
<mapping resource="com/ebook/dao/LenderRecord.hbm.xml" />
<mapping resource="com/ebook/dao/ReturnRecord.hbm.xml" />

</session-factory>
</hibernate-configuration>

==========================================================
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

=============================================
struts-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="userinfo" type="com.demo.actionform.User"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>

<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="userinfo"
scope="request">
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.demo.struts.ApplicationResources" />
</struts-config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值