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

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.