Struts+Hibernate+Spring组合相关的文章也很多了,本篇内容和Struts2.1+Hibernate3.1+Spring2.0组合相关。其实对于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> 分享题目:Struts+Hibernate+Spring组合简单范例
分享路径:http://www.shufengxianlan.com/qtweb/news13/424413.html网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联