免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3072 | 回复: 0

Spring Struts整合方案汇总小结 [复制链接]

论坛徽章:
0
发表于 2010-01-14 11:09 |显示全部楼层



Spring和Struts整合的方案有很多种,整理如下:
第一种 Listener方式
将Spring服务作为web容器的Listener,随web服务器启动而启动
   1、需要在web.xml中配置
Java代码

  •    
  • class>org.springframework.web.context.ContextLoaderListenerclass>   
  •   
    org.springframework.web.context.ContextLoaderListener
       2、然后在Struts的action中通过
    Java代码

  • ServletContext sc = this.getServlet().getServletContext();   
  • WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);   
  • ctx.getBean("xxx");  ServletContext sc = this.getServlet().getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    ctx.getBean("xxx");
    完成托管Bean的获取。
    第二种 Load on Startup Servlet方式
    将Spring服务作为web容器的load on startup Servlet,随web服务器启动而启动,这种方式和第一种基本是一样的,只是spring加载的时间会比第一种晚一些,servlet2.3标准,listener的加载早于startup servlet的加载
       1、这种方式需要在web.xml中配置
    Java代码

  •    
  • contextLoader   
  • class>org.springframework.web.context.ContextLoaderServletclass>   
  • 1   
  •   
    contextLoader
    org.springframework.web.context.ContextLoaderServlet
    1
       2、这种整合方案,获取bean的方法,和第一种方式相同
    第三种 Struts Plugin+Spring DelegatingRequestProcessor方式
    前两种方式,都没有将struts的action纳入spring管理,接下来的要说的两种方式比较类似,放在一起说明,都是可以将Struts的action纳入到Spring管理的方式。
       1、通过Struts的Plugin方式,在应用启动时加载Spring容器,既然是Struts的Plugin,当然是在struts-xxx.xml文件中进行配置,增加如下Plugin:
    Java代码

  • "org.springframework.web.struts.ContextLoaderPlugIn">   
  • "contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext_xxx.xml"/>   
  •   
       2、Struts是通过ActionServlet处理请求的,而代码实现上是通过RequestProcessor进行处理的,通过Spring的RequestProcessor子类-DelegatingRequestProcessor,可以替代原有的Struts请求处理方式,从而转到Spring容器中进行处理。因此需要更改原有的Struts RequestProcessor行为,使用Spring自己的DelegatingRequestProcessor,这需要在struts-xxx.xml中配置,增加如下配置:
    Java代码

  • "org.springframework.web.struts.DelegatingRequestProcessor">  
       3、经过上面的配置,现在struts的action已经交给spring来管理了,spring的DelegatingRequestProcessor会处理web请求,将请求转发给struts-xxx.xml中定义的action,而这些action又是通过spring管理的,因此原来的struts配置文件中的action配置:...中的type就可以省略了,改成...,而type的设置则放到spring的配置文件中,上文中指定了两个spring配置文件/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext_xxx.xml,我们在其中一个文件中加入:
    Java代码

  • "/login" class="com.soar.loginAction" singleton="false">   
  • "xx">   
  •    just a String property   
  •    
  •    
  •   

       just a String property


    这和对待普通的bean没有什么区别了,但是名字是struts配置文件中指定的path指定的值"/login",设置singleton="false"是每请求一次,生成一个action(和struts1默认策略不同)
    这样就完成了spring与struts的整合以及spring管理action


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/69320/showart_2148617.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP