struts+spring+hibernate的集成
struts+spring+hibernate的集成
1、struts和spring的集成
* 将spring作为插件集成到struts中,到struts-config.xml中进行配置:
<!-- 配置插件,将spring集成到struts中 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<!-- 指定spring配置文件的路径 -->
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml" />
</plug-in>
* <action>的type指定成代理类
<action path="/user"
type="org.springframework.web.struts.DelegatingActionProxy"
name="userForm"
scope="request"
parameter="op">
<forward name="success" path="/success.jsp" />
</action>
* 到spring的配置文件中配置Action
<!-- action -->
<bean name="/user" class="com.westaccp.user.web.actions.UserAction">
<property name="userBiz" ref="userBiz" />
</bean>
2、spring和hibernate的集成
* 让spring管理SessionFactory
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
* 让spring管理Session
让dao类继承自HibernateDaoSupport类
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao
* 让spring管理事务(声明式事务)
让spring的AOP将事务织入
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="txAdvice">
<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="bizMethods" expression="execution(* com.westaccp.user.biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
3、中文处理
spring提供了一个中文处理过滤器org.springframework.web.filter.CharacterEncodingFilter
配置过滤器,到web.xml中进行配置
<!-- 配置中文处理过滤器(由spring提供) -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4、关于hibernate延迟加载的问题,用spring提供的OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
<!-- 指定spring配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--
配置ContextLoaderListener
ContextLoaderListener作用:根据spring的配置文件,创建BeanFactory
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置OpenSessionInViewFilter -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5、注意:spring中action的配置问题
<bean name="/user" class="com.westaccp.user.web.actions.UserAction" scope="singleton | prototype">
singleton:多个线程访问的是同一个Action的实例
prototype:spring为每一个线程创建Action的一个实例,这样能够解决线程安全问题
默认情况下scope="singleton"
分享到:
相关推荐
SSH集成环境配置[汇编].pdf
NULL 博文链接:https://xwm329.iteye.com/blog/1323239
最新SSH集成_2016-11-08.txt 环境:struts2.5.5+spring4.3.3+hiberante5.2.4 真正的集成,struts action交给spring托管。真实的项目工程,带数据库初始化脚本。
自己写的插件,目前只能添加struts2.1.8,spring2.5,hibernate3.2,环境只添加了ssh的必须包,可以创建一个纯净的ssh项目 这个版本只考虑了web路径为WebContent的情况,忘了考虑WebRoot的情况了,如有需要请下载...
SSH2集成开发环境,包括spring、struts2、hibernate所需要的jar文件。
基于ssh开发的demo,环境搭建,类库导入,方便开发ssh。。
ssh集成开发的OA系统 系统开发采用先进的开发软件与技术,即采用Dreamweaver页面制作软件与Photoshop等技术相结合开发系统的页面,用JSP动态页面设计技术与Struts、Spring、Hibernate技术,MySQL数据库管理系统开发...
Strust+spring+hibernate新闻发布系统 Strust+spring+hibernate新闻发布系统 Strust+spring+hibernate新闻发布系统
把文件拷贝到Eclipse中plugins目录下,打开Eclipse,创建web项目,右键项目弹出菜单会出现一...自己写的插件,目前只能添加struts2.1.8,spring2.5,hibernate3.2,环境只添加了ssh的必须包,可以创建一个纯净的ssh项目
MyEclips集成ssh环境搭建。一些说明和对整个集成过程的DEMO。
SSH的整合问题集中处理 <!-- 事务配置 --> <!-- JTA环境的事务配置 WebLogicJtaTransactionManager"/> --> <!-- 使用annotation定义事务 --> <!-- 保证POJO中标注@Required的属性被注入 --> ...
里面包含一个Struts2 Spring,hibernate框架的注册实例,希望对刚解除ssh环境配置的有帮助,刚配的时候确实有点困扰
本文档详细地描述了SSH的集成过程,通过本文档你可以搭建web开发的环境。
自己搭建的ssh基本集成环境 数据库是Oracle10g 数据源 c3p0
tomcat6.0下运行,myeclipse开发环境,jdk1.6Web项目请假流实例!架包齐全,可运行!
SSH框架集成环境的一些心得体会及自己做过的一些项目,对初学者很有用。
红袜子SSH最新集成 第一阶段;重点完成搭建环境
SSH2项目环境搭建步骤详解,SSH2项目搭建过程中的每一步步骤详解
ssh环境搭建,采用struts2、hibernate4、spring3搭建环境、实现了邮件发送功能、验证码登录功能、上传下载功能、dwr集成、报表导出功能
这是关于ssh的集成练习小项目 有关于ssh的环境搭建以及使用的方法