这篇文章给大家分享的是有关Struts+Hibernate+Spring如何组合使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
其实对于S2SH的组合,其实注意几点就可以了:
第一: 由于Struts2的Action是有状态,而Spring中的Bean默认为单例模式的,故一定要将Spring中配置Struts2的Action的scope设置为prototype 。而Struts1中的Action是无状态故在SSH时可以不用将Spring设置为prototype!
第二:Struts2与Spring的组合不像Struts1,它需要一个Struts2中一个struts2-spring-plugin-2.1.6.jar也不需要在struts.xml中加入其它的配置,只需将struts.xml中的action的class的值改成action在Spring中配置的Bean的id 就可以了!
简单的Struts+Hibernate+Spring例子如下:
(说明这个例子当中包括Struts2大部分的内容,主要是想体验一下Struts2的功能)
数据库如下:uid,ufristName,ulastName,uage四个字段
一,加入Spring的支持
二,加入Hibernate的支持(Hibernate的配置由Spring去管理)
三,加入Struts2的支持
四,加入Struts2对Spring的插件(struts2-spring-plugin-2.1.6.jar)
在web.xml中加入对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); /** * 导出用户列表的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 "> < !-- 数据源 --> < 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> < !-- 事务管理器 --> < bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name="sessionFactory"> < ref bean="MySessionFactory">< /ref> < /property> < /bean> < !-- 定义事务的属性 --> < 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切面中配置事务 --> < 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> < !-- 对于Struts2与Spring集成时,Strtus2的Action是有状态的故这个的scope必须为prototype,Spring默认的是单例模式 --> < 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导出 --> < action name="exportUsers" class="exportUsersAction"> < result name="success" type="stream"> < param name="contentType">application/vnd.ms-excel< /param> < !-- 对于第一个参数默认值为inline这样的话若在线打开的话会生成两个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导出< /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>
感谢各位的阅读!关于“Struts+Hibernate+Spring如何组合使用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。