配置式事务:struts与spring的二种整合方式

本文介绍了一种将Struts与Spring框架整合的方法,包括通过配置文件管理Action类、使用Spring的代理类处理请求以及整合所需的步骤。此外还提供了一种替代方案,即使用单独的配置文件来管理Action。

 

    使用两个配置文件:applicationContext.xml和hibernate.cfg.xml
   

   1.新建web项目
  
   2.添加hibernate支持和spring支持 (顺序可以颠倒)
  
   3.使用工具支持进行数据库的反射操作
  
   4.编写dao接口,实现dao接口
  
  
   5.修改spring配置文件,管理持久层
  
  
   6.编写biz接口和实现接口(不编写事务代码)
  
   7.修改spring配置文件,管理业务层代码
  
   (针对业务接口进行测试)
  
   8.添加struts支持,实现mvc架构:acton类,formbean类,jsp页面等
  
   9.修改spring配置文件,管理action类,
     <bean name="/booklist" class="com.struts.action.BooklistAction">
  <property name="bookinfoBiz" ref="BookinfoBizImpl"></property>
  </bean>
  
   10.修改struts配置文件,把action配置的类修改成委托代理类
     <action parameter="list" path="/booklist"
  type="org.springframework.web.struts.DelegatingActionProxy"
  validate="false">
  <forward name="success" path="/booklist.jsp" />
 </action>
 
 
 (三个框架整合完毕)
 
 11.把spring交给web服务器管理,修改web.xml
 
   <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/classes/applicationContext.xml
   </param-value>
  </context-param>
 
  <listener>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>
  
 
 12.如果使用了延迟加载数据.解决延迟加载异常,修改web.xml
   <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>
  
  
  
==================================================================

   struts与spring的第二种整合方式:
    
     1.编写一个spring配置文件action-servlet,用于管理action类
    
        <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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
  
   <!-- 表示层 -->
  
   <bean name="/booklist" class="com.struts.action.BooklistAction">
    <property name="bookinfoBiz" ref="BookinfoBizImpl"></property>
   </bean>
  
  </beans>
  
 2.修改struts-config.xml,配置中央控制器的处理类
 
   <!-- 配置中央控制器的处理类;不使用ActionServlet类 -->
  <controller>
   <set-property property="processorClass"
    value="org.springframework.web.struts.DelegatingRequestProcessor" />
  </controller>
  
   3.添加一个struts插件,用于读取spring的action类配置文件
  
     <!-- struts插件类,用于读取spring配置文件 -->
  <plug-in
   className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property property="contextConfigLocation"
    value="/WEB-INF/classes/action-servlet.xml" />
  </plug-in>
  
 4.把前面配置的struts与spring的配置代码去掉
   


  
  
 
  
  
  
    

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值