這篇文章給大家分享的是有關(guān)Struts+Hibernate+Spring如何組合使用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站主營白朗網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),白朗h5小程序定制開發(fā)搭建,白朗網(wǎng)站營銷推廣歡迎白朗等地區(qū)企業(yè)咨詢
其實(shí)對(duì)于S2SH的組合,其實(shí)注意幾點(diǎn)就可以了:
第一: 由于Struts2的Action是有狀態(tài),而Spring中的Bean默認(rèn)為單例模式的,故一定要將Spring中配置Struts2的Action的scope設(shè)置為prototype 。而Struts1中的Action是無狀態(tài)故在SSH時(shí)可以不用將Spring設(shè)置為prototype!
第二:Struts2與Spring的組合不像Struts1,它需要一個(gè)Struts2中一個(gè)struts2-spring-plugin-2.1.6.jar也不需要在struts.xml中加入其它的配置,只需將struts.xml中的action的class的值改成action在Spring中配置的Bean的id 就可以了!
簡單的Struts+Hibernate+Spring例子如下:
(說明這個(gè)例子當(dāng)中包括Struts2大部分的內(nèi)容,主要是想體驗(yàn)一下Struts2的功能)
數(shù)據(jù)庫如下:uid,ufristName,ulastName,uage四個(gè)字段
一,加入Spring的支持
二,加入Hibernate的支持(Hibernate的配置由Spring去管理)
三,加入Struts2的支持
四,加入Struts2對(duì)Spring的插件(struts2-spring-plugin-2.1.6.jar)
在web.xml中加入對(duì)Spring和Struts2的配置:
< ?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"> < 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>struts2< /filter-name> < filter-class> org.apache.struts2.dispatcher.FilterDispatcher < /filter-class> < /filter> < 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>
UsersDAO的接口如下:
public interface UsersDaoInter { public abstract void save(Users transientInstance); public abstract void delete(Users persistentInstance); public abstract void delete(final int uId); public abstract Users findById(java.lang.Integer id); public abstract List findByExample(Users instance); public abstract List findByProperty(String propertyName, Object value); public abstract List findAll(); public abstract Users merge(Users detachedInstance); }
UsersService接口如下:
public interface UsersServiceInter { public abstract void save(Users transientInstance); public abstract void delete(Users persistentInstance); public abstract void delete(final int uId); public abstract Users findById(java.lang.Integer id); public abstract List findByExample(Users instance); public abstract List findByProperty(String propertyName, Object value); public abstract List findAll(); public abstract Users merge(Users detachedInstance); /** * 導(dǎo)出用戶列表的Excel * @return */ public abstract InputStream exportUsers(); }
Spring的applicationContext.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 "> < !-- 數(shù)據(jù)源 --> < bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource"> < property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"> < /property> < property name="url" value="jdbc:microsoft:sqlserver://localhost:1433"> < /property> < property name="username" value="sa">< /property> < /bean> < !-- SessionFactory配置 --> < bean id="MySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> < property name="dataSource"> < ref bean="MyDataSource">< /ref> < /property> < property name="hibernateProperties"> < props> < prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect < /prop> < /props> < /property> < property name="mappingResources"> < list> < value>com/mengya/entity/Users.hbm.xml< /value> < /list> < /property> < /bean> < !-- 事務(wù)管理器 --> < bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name="sessionFactory"> < ref bean="MySessionFactory">< /ref> < /property> < /bean> < !-- 定義事務(wù)的屬性 --> < tx:advice id="txAdvice" transaction-manager="transactionManager"> < tx:attributes> < tx:method name="save*" propagation="REQUIRED" /> < tx:method name="delete*" propagation="REQUIRED" /> < tx:method name="merge*" propagation="REQUIRED" /> < tx:method name="*" read-only="false" /> < /tx:attributes> < /tx:advice> < !-- 使用Spring的AOP管理Hibernate的Transaction --> < aop:config> < !-- 定義AOP切面 --> < aop:pointcut id="allManagerMethod" expression="execution(* com.mengya.service.*.*(..))" /> < !-- 在AOP切面中配置事務(wù) --> < aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> < /aop:config> < !-- 如下是DAO,Service,Action的配置 --> < bean id="UsersDAO" class="com.mengya.dao.imple.UsersDAO"> < property name="sessionFactory"> < ref bean="MySessionFactory">< /ref> < /property> < /bean> < bean id="userService" class="com.mengya.service.imple.UsersService"> < property name="usersdao"> < ref bean="UsersDAO">< /ref> < /property> < /bean> < !-- 對(duì)于Struts2與Spring集成時(shí),Strtus2的Action是有狀態(tài)的故這個(gè)的scope必須為prototype,Spring默認(rèn)的是單例模式 --> < bean id="saveUserAction" class="com.mengya.usersAction.SaveUsersAction" scope="prototype"> < property name="usersService"> < ref bean="userService">< /ref> < /property> < /bean> < bean id="listUserAction" class="com.mengya.usersAction.ListUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService">< /ref> < /property> < /bean> < bean id="deleteUserAction" class="com.mengya.usersAction.DeleteUsersAction" scope="prototype"> < property name="usersService"> < ref bean="userService">< /ref> < /property> < /bean> < bean id="updateUserAction" class="com.mengya.usersAction.UpdateUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService">< /ref> < /property> < /bean> < bean id="exportUsersAction" class="com.mengya.usersAction.ExportUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService">< /ref> < /property> < /bean> < /beans>
struts.xml配置如下:
< !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts> < package name="mengya" extends="struts-default"> < action name="saveUser" class="saveUserAction"> < result name="success" type="redirectAction">listUser.action< /result> < result name="input">/add2.jsp< /result> < /action> < action name="listUser" class="listUserAction"> < result name="success">/userList.jsp< /result> < /action> < action name="deleteUser" class="deleteUserAction"> < result name="success" type="redirectAction">listUser.action< /result> < /action> < action name="getUser" class="updateUserAction" method="getUserById"> < result name="success">/view.jsp< /result> < /action> < action name="updateUser" class="updateUserAction" method="updateUser"> < result name="success" type="redirectAction">listUser.action< /result> < result name="input">/view.jsp< /result> < /action> < !-- 將用戶信息用Excel導(dǎo)出 --> < action name="exportUsers" class="exportUsersAction"> < result name="success" type="stream"> < param name="contentType">application/vnd.ms-excel< /param> < !-- 對(duì)于第一個(gè)參數(shù)默認(rèn)值為inline這樣的話若在線打開的話會(huì)生成兩個(gè)xls文件 --> < param name="contentDisposition">attachment;filename="allUsers.xls"< /param> < param name="inputName">downloadFile< /param> < /result> < /action> < /package> < /struts>
jsp頁面:用戶列表
< table border="1" align="center" width="60%"> < tr> < td colspan="6" align="center"> < s:text name="UserListInfo">< /s:text> < /td> < /tr> < tr> < td> < s:text name="UId">< /s:text> < /td> < td> < s:text name="UFristName">< /s:text> < /td> < td> < s:text name="ULastName">< /s:text> < /td> < td> < s:text name="UAge">< /s:text> < /td> < td> < s:text name="UpdateOperaction">< /s:text> < /td> < td> < s:text name="DeleteOperaction">< /s:text> < /td> < /tr> < s:iterator value="#request.listUser" var="user"> < tr> < td> < s:property value="#user.uid"/> < /td> < td> < s:property value="#user.ufristName"/> < /td> < td> < s:property value="#user.ulastName"/> < /td> < td> < s:property value="#user.uage"/> < /td> < td> < s:a href="getUser.action?users.uid=%{#user.uid }"> < s:text name="UpdateOperaction">< /s:text> < /s:a> < /td> < td> < s:a href="deleteUser.action?uid=%{#user.uid }"> < s:text name="DeleteOperaction">< /s:text> < /s:a> < /td> < /tr> < /s:iterator> < /table> < tr/> < tr/> < tr/> < div align="center"> < s:a href="exportUsers.action">Excel導(dǎo)出< /s:a> < /div>
用戶信息修改頁面:
< s:form action="updateUser" method="post"> < s:hidden name="users.uid">< /s:hidden> < s:textfield name="users.ufristName" key="UFristName">< /s:textfield> < s:textfield name="users.ulastName" key="ULastName">< /s:textfield> < s:textfield name="users.uage" key="UAge">< /s:textfield> < s:submit key="submit">< /s:submit> < /s:form>
感謝各位的閱讀!關(guān)于“Struts+Hibernate+Spring如何組合使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文標(biāo)題:Struts+Hibernate+Spring如何組合使用
轉(zhuǎn)載來于:http://jinyejixie.com/article40/ggijho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、Google、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)