struts2+spring+hibernate整合

來源:互聯網
上載者:User

 

1.struts.xml配置

 

<?xml version="1.0" encoding="UTF-8" ?><br /><!DOCTYPE struts PUBLIC<br /> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<br /> "http://struts.apache.org/dtds/struts-2.0.dtd"><br /><struts></p><p><!-- 配置struts2中的Action對象交給spring建立 --><br /><constant name="struts.objectFactory" value="spring"/></p><p><!-- 配置本屬性,可以使得改動struts設定檔不用重啟應用伺服器 --><br /><constant name="struts.configuration.xml.reload" value="true"/></p><p> <package name="crm" namespace="/" extends="struts-default"></p><p> <global-results><br /> <result name="pub_add_success"><br /> /common/pub_add_success.jsp<br /> </result><br /> <result name="pub_del_success"><br /> /common/pub_del_success.jsp<br /> </result><br /> </global-results></p><p> <!-- 使用路徑模式比對的方法,減輕配置工作量 --><br /> <action<br /> name="*Customer"<br /> class="{1}CustomerAction"<br /> ><br /> <result name="index">/struts2/customer/index.jsp</result><br /> <result name="input">/struts2/customer/{1}_input.jsp</result><br /> <result name="import_input">/struts2/customer/import_input.jsp</result><br /> <result name="import_result">/struts2/customer/import_result.jsp</result><br /> </action><br /> </package><br /></struts><br />

 

2.applicationContext-common.xml

 

<?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans</p><p>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</p><p>http://www.springframework.org/schema/context</p><p>http://www.springframework.org/schema/context/spring-context-2.5.xsd</p><p>http://www.springframework.org/schema/aop</p><p>http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</p><p>http://www.springframework.org/schema/tx</p><p> http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><br /><!-- 建立資料來源 --><br /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><br /><!-- 資料庫驅動,我這裡使用的是Mysql資料庫 --><br /><property name="driverClassName" value="com.mysql.jdbc.Driver"><br /></property><br /><!-- 資料庫地址,這裡也要注意一下編碼,不然亂碼可是很鬱悶的哦! --><br /><property name="url" value="jdbc:mysql://localhost:3306/s2sh01"><br /></property><br /><!-- 資料庫的使用者名稱 --><br /><property name="username" value="root"></property><br /><!-- 資料庫的密碼 --><br /><property name="password" value="root"></property></p><p></bean><br /><!-- 配置sessionFactory --></p><p><bean id="sessionFactory"<br />class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><br /><!-- 把資料來源注入給Session工廠 --><br /><property name="dataSource" ref="dataSource" /><br /><property name="hibernateProperties"><br /><props><br /><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect<br /></prop><br /><prop key="hibernate.show_sql">true</prop><br /><prop key="hibernate.hbm2ddl.auto">update</prop><br /><!--<br /><prop key="hibernate.cache.use_second_level_cache">true</prop><br /><prop<br />key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop><br /><prop key="hibernate.cache.use_query_cache">true</prop><br />--><br /></props><br /></property><br /><!-- 配置對應檔 --><br /><property name="mappingResources"><br /><list><br /><value>com/fct/s2sh/model/User.hbm.xml<br /></value><br /></list><br /></property><br /></bean><br /><!-- 把Session工廠注入給hibernateTemplate --><br /><!--<br />解釋一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在執行時自動建立<br />HibernateCallback 對象,例如:load()、get()、save、delete()等方法。<br />--><br /><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><br /><property name="sessionFactory" ref="sessionFactory"></property><br /></bean></p><p><!-- 配置交易管理員 --><br /><bean id="transactionManager"<br />class="org.springframework.orm.hibernate3.HibernateTransactionManager"><br /><property name="sessionFactory"><br /><ref bean="sessionFactory" /><br /></property><br /></bean></p><p><!-- 配置事務的傳播特性 --><br /><tx:advice id="txAdvice" transaction-manager="transactionManager"><br /><tx:attributes><br /><tx:method name="add*" propagation="REQUIRED" /><br /><tx:method name="del*" propagation="REQUIRED" /><br /><tx:method name="modify*" propagation="REQUIRED" /><br /><tx:method name="*" read-only="true" /><br /></tx:attributes><br /></tx:advice></p><p><!-- 那些類的哪些方法參與事務 --><br /><aop:config><br /><aop:pointcut id="allManagerMethod"<br />expression="execution(* com.fct.s2sh.service.*.*(..))" /><br /><aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /><br /></aop:config><br /></beans>

3.applicationContext-actions.xml

 

 <?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"<br />xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans</p><p>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</p><p>http://www.springframework.org/schema/context</p><p>http://www.springframework.org/schema/context/spring-context-2.5.xsd</p><p>http://www.springframework.org/schema/aop</p><p>http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</p><p>http://www.springframework.org/schema/tx</p><p> http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><br /><!-- --><br /><bean id="userAction" class="com.fct.s2sh.web.struts2.UserAction" scope="prototype"><br /><property name="userManager" ref="userManager" /><br /></bean></p><p><bean id="userManager" class="com.fct.s2sh.service.impl.UserManagerImpl"><br /><property name="userDAO" ref="userDAO"> </property><br /><!-- <property name="sessionFactory" ref="sessionFactory"></property> --><br /></bean></p><p><bean id="userDAO" class="com.fct.s2sh.dao.impl.UserDAOImpl"><br /><property name="sessionFactory" ref="sessionFactory"></property><br /></bean><br /></beans>

 

 

4.web.xml

 

 

 <?xml version="1.0" encoding="UTF-8"?><br /><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://java.sun.com/xml/ns/javaee<br />http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><br /><welcome-file-list><br /><welcome-file>index.jsp</welcome-file><br /></welcome-file-list><br /><!--<br />Spring 的ApplicationContext設定檔的路徑,可使用萬用字元,applicationContext*.xml表示所有以applicationContext開頭的xml檔案。<br />context-param是在容器啟動後最先被執行的,並且被放入到容器上下文中。在這裡引入spring的設定檔,<br />是供Spring的ContextLoaderListener監聽器使用。而這個監聽器中會有一個ContextLoade類用來擷取這個設定檔中的資訊。<br />從而進行Spring容器的初始化工作。<br />--><br /><context-param><br /><param-name>contextConfigLocation</param-name><br /><param-value>classpath*:applicationContext-*.xml</param-value><br /></context-param><br /> <!-- 這個監聽器就是為了讀取Spring的設定檔,這在上面已經講到了。 --><br /><listener><br /><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><br /></listener></p><p><!-- 這是Spring提供的一個用來防止記憶體流失的監聽器。在我們使用struts2架構,或其它的某些類庫時,因為它們自身的設計,<br />會用到Introspector(內省)機制來擷取Bean對象的資訊。但不幸的是,這些架構或類庫在分析完一個類之後卻沒有將它從記憶體中清除掉,<br />記憶體中還保留有大量的靜態資源,而這些東西又無法進行記憶體回收,因此產生了很嚴重的記憶體流失問題。直接表現為伺服器的記憶體使用量會隨著時間而不斷上升,<br />最後的結果當然就是伺服器當掉。所以在這裡加入此監聽器,能夠協助我們更好的處理記憶體資源回收的問題。<br /> --><br /> <listener><br /> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class><br /> </listener><br /> <!-- 這是Spring的編碼過濾器,我們可以直接拿來用,相信這段配置應該很好理解,不過請大家注意forceEncoding這個參數,<br /> 把它設定為true表示不管請求中的編碼是什麼格式,都將強制採用encoding中設定的編碼方式。另外對於響應也將按照encoding指定的編碼進行設定。<br /> 另外不建議將編碼設定成gb2312或是gbk格式,請採用基於Unicode的UTF-8編碼 --><br /><filter><br /><filter-name>Spring character encoding filter</filter-name><br /><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><br /><init-param><br /><param-name>encoding</param-name><br /><param-value>UTF-8</param-value><br /></init-param><br /></filter><br /><filter-mapping><br /><filter-name>Spring character encoding filter</filter-name><br /><url-pattern>/*</url-pattern><br /></filter-mapping><br /><!-- 這個過濾器是個好東西,有了它,我們在使用Hibernate消極式載入的時候,就不會再為因Session被關閉,<br />導致消極式載入資料異常而頭痛了。網上有很多人說這個不好,其實在使用中,效果還是不錯的。<br /> --><br /><filter><br /><filter-name>hibernateFilter</filter-name><br /><filter-class><br />org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><br /></filter><br /><filter-mapping><br /><filter-name>hibernateFilter</filter-name><br /><url-pattern>/*</url-pattern><br /></filter-mapping></p><p><!-- struts2 --><br /><filter><br /><filter-name>struts2</filter-name><br /><filter-class><br />org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><br /></filter></p><p><filter-mapping><br /><filter-name>struts2</filter-name><br /><url-pattern>/*</url-pattern><br /></filter-mapping><br /><!-- 如果不想出現莫名其妙的錯誤,請控制好這些過濾器映射的順序。 --><br /></web-app><br />

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.