SSH框架整合

开发环境:
  IDE:MyEclipse7.0
  SSH:Struts1.2+Hibernate3.2+Spring2.0


整合方法:
1. 建立工程,添加Struts支持,与单独用Struts做开发的配置没什么不同,基本上就是一直“下一步”。
2. 添加Hibernate,当向导执行到配置SessionFactory时可跳过,因为SessionFactory最后由Spring负责生成。

3. 添加Spring,我选择的是2.0,当然也可以选择2.5,配置上大同小异。需要注意的是至少应引入一下四个库:Spring AOP Libraries、Spring Core Libraries、Spring Persistence Libraries和Spring Web Libraries,在接下来的步骤中不要忘了选中 “Create Spring SessionFactory”。

4 .Struts1.2和Spring2.0的整合

    1)在web.xml中加入如下代码令服务器自动加载Spring

Xml代码
  1. < context-param >   
  2.         < param-name > contextConfigLocation</ param-name >   
  3.         < param-value > /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</ param-value >   
  4.   </ context-param >   
  5.  < listener >   
  6.         < listener-class > org.springframework.web.context.ContextLoaderListener</ listener-class >   
  7.     </ listener >   

   2)当在struts-config.xml定义action时,需要将action的类型修改为

     “org.springframework.web.struts.DelegatingActionProxy”

       同时在Action的类中还需要提供所调用的业务对象的setter方法,让spring为其注入依赖的对象。

  3)在spring的配置文件中定义action的bean,其中的name属性值必须与struts配置文件的path属性值相一致。

     (建议将 action的scope属性值设置成"prototype",这样就避免了一些安全问题)

5. Spring2.0和Hibernate3.2的整合

  1)所有的DAO类都必须继承HibernateDaoSupport基类,在这个类中可以获得Hibernate的各种核心接口(如Session、SessionFactory等),同时在配置时需要注入sessionFactory。

    注:默认情况下Hibernate只有遇到运行时异常数据库操作才会回滚,普通异常不会回滚。

  2)在Spring中配置SessionFactory,默认情况下MyEclipse已经为我们配置好了。

  3)配置声明式事务,首先需要配置事务管理器

Xml代码
  1. < bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >   
  2.     < property  name ="sessionFactory" >   
  3.                  < ref  local ="sessionFactory" />           
  4.         </ property >   
  5. </ bean >    

   接下来需要配置事务管理的AOP

 

Xml代码
  1. <!--定义事务的传播特性-->   
  2. < tx:advice  id ="txAdvice"  transaction-manager ="transactionManager" >   
  3.   < tx:attributes >   
  4.     < tx:method  name ="add*"  propagation ="REQUIRED" />   
  5.     < tx:method  name ="delete*"  propagation ="REQUIRED" />   
  6.     < tx:method  name ="update*"  propagation ="REQUIRED" />   
  7.     < tx:method  name ="*"  read-only ="true" />   
  8.   </ tx:attributes >   
  9. </ tx:advice >   
  10.   
  11. <!--AOP的配置-->   
  12. < aop:config >   
  13.   < aop:pointcut  id ="managersMethod"  expression ="execution(* com.test.manager.*.*(..))" />   
  14.   < aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="managersMethod" />   
  15. </ aop:config >   

   需要注意的是,默认情况下MyEclipse没有引入相关的xmlSchema,也没有定义相关的命名空间,所以我们需要手动配置

 

Xml代码
  1. xmlns ="http://www.springframework.org/schema/beans"   
  2.     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:aop ="http://www.springframework.org/schema/aop"   
  4.     xmlns:tx ="http://www.springframework.org/schema/tx"   
  5.     xsi:schemaLocation ="http://www.springframework.org/schema/beans  
  6.                                       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.                                    http://www.springframework.org/schema/aop   
  8.                                       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.                                       http://www.springframework.org/schema/tx   
  10.                                       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  

   5. OpenSessionInView的处理

写道
在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception。
Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题。它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具 体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。
Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。

    详见:toxy   和    downpour    对该问题的相关文章

    其中spring提供了filter的处理方法,配置方法如下:

Xml代码
  1. < filter >   
  2.    < filter-name > hibernateFilter</ filter-name >   
  3.    < filter-class > org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</ filter-class >   
  4.  </ filter >   
  5. < filter-mapping >   
  6.    < filter-name > hibernateFilter</ filter-name >   
  7.    < url-pattern > /*</ url-pattern >   
  8.  </ filter-mapping >     

 6. 对表单中中文的处理,spring中为我们提供了Encoding Filter,配置如下

 

Xml代码
  1. < filter >   
  2.     < filter-name > Spring character encoding filter</ filter-name >   
  3.     < filter-class > org.springframework.web.filter.CharacterEncodingFilter</ filter-class >   
  4.     < init-param >   
  5.         < param-name > encoding</ param-name >   
  6.         < param-value > GBK</ param-value >   
  7.     </ init-param >   
  8.   </ filter >   
  9.  < filter-mapping >   
  10.     < filter-name > Spring character encoding filter</ filter-name >   
  11.     < url-pattern > /*</ url-pattern >   
  12.   </ filter-mapping >   

 

问题处理

  在配置完成后,进行测试时可能会产生一些问题,如

   java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
./=?KHsQ  HK$;xo%5  
  该问题的解决方法如下: ~@^$j  
wo+i^-zE    去掉类路径上的关于Hibernate的3个lib N} $:,R}  
  asm.jar  
  asm-attrs.jar  
  cglib-2.1.3.jar  
<moLSm~B|K Rm oG/`x~ jb]k`$mW~
加入Spring中的以下4个lib JrM~POo+  
  asm-2.2.3.jar  
  asm-commons-2.2.3.jar  
  asm-util-2.2.3.jar  
  cglib-nodep-2.1_3.jar
o[j(~$@P8 4n:L7/u ]`Cga&Rh e2jZ{s[ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值