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

首先是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整合完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章