strtus2.1.6+hibernate3.2+sping2.5整合方法

14 篇文章 1 订阅
5 篇文章 1 订阅

首先是Spring和Hibernate进行整合

新建一个Web项目

导入Spring的开发包

项目名称右键-->Myeclipse-->add hibernate capabilities-->选中Spring2.5,全选所有包,底下的选copy lib。。。-->finish

相同的方法添加hibernate的支持包

Struts2.1.6的配置包

struts2-core-2.1.6.jar

xwork-2.1.2.jar

commons-logging-1.0.4.jar

freemarker-2.3.13.jar

ognl-2.6.11.jar

commons-fileupload-1.2.1.jar拷贝到Web-inf下的lib文件夹中

配置完后applicationContex.xml的内容为

<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<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.5.xsd
   http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="org.gjt.mm.mysql.Driver">
  </property>
  <property name="url" value="jdbc:mysql://localhost:3306/mldn"></property>
  <property name="username" value="root"></property>
  <property name="password" value="13523345289xiao"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/xly/crm/pojo/Employee.hbm.xml</value></list>
  </property></bean>
  
  <bean id="employeeDao"

class="com.xly.crm.dao.hibernate.EmployeeDaoHibernate">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
  </bean>
  
  <bean id="employeeManager"

class="com.xly.crm.service.impl.EmployeeManagerImpl">
   <property name="employeeDao">
    <ref bean="employeeDao"/>
   </property>
  </bean>
  
  
<bean id="addBean" class="com.xly.crm.action.EmployeeAction"

scope="prototype">
   <property name="employeeManager">
    <ref bean="employeeManager"/>
   </property>
  </bean>
  
  <bean id="listBean" class="com.xly.crm.action.EmployeeAction"

scope="prototype">
   <property name="employeeManager">
    <ref bean="employeeManager"/>
   </property>
  </bean>
  <!-- 配置事务管理器 -->
  <bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
  </bean>
  <!-- 配置事务的传播特性 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED"/>
   <tx:method name="delete*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 <!-- 那些类的哪些方法参与事务 -->

 <aop:config>
  <aop:pointcut id="allManagerMethod" expression_r="execution (*

com.*.*.service.*.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
  
 </aop:config>
  <!-- 让Spring通过自动扫描来查询和管理Bean -->
    
<context:component-scan base-package="com.xly"/>

  </beans>

注意:1,hibernate3.2与Spring2.5整合的时候应该删除asm.2.2.3.jar 包,不删除的话可能出现

Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V的错误

2, <context:component-scan base-package="com.xly"/>

        这样配置之后,就省去了上面注释掉的DAO层和Service层等配置代码

web.xml的内容为

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 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">
 //上下文参数
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 //监听器
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 //懒加载过滤器
 <filter>
  <filter-name>lazyLoadingFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 //Struts2的过滤器
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>lazyLoadingFilter</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在src目录下新建一个文件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.objectFactory告诉struts的action实现全部有Spring处理
<constant name="struts.objectFactory" value="spring"></constant>

</package>
</struts>

由此ssh整合完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值