Spring与Struts2整合

    在此之前,总以为一个项目里面只要包含了Struts2、Spring和Hibernate的jar就算是一个SSH框架了,现在随着程序编写所遇到的问题,才发现这个想法很可笑,当然,现在的见解也是很肤浅。

   之所以会了解到这步,是发现,Struts2和jsp页面交互的数据变量的出现重叠,进而发现action的生命周期是需要设定的,但是无法在Struts.xml里配置,需要在通过Spring来管理,可以annotation也可以xml。

    首先,要在web.xml里添加:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  来监听applicationContext.xml

接下来,在struts.xml里添加:

<constant name="struts.objectFactory" value="spring"/>

用以和spring关联

     然后,就可以在Spring里来注册要用到的Action对象了,可以给它们设定scope。一般都是scope="prototype",这样每次调用都能新建一个action,防止上次的参数遗留。

     当然,action也可以像service和Imp样通过annotation来注入。 

     !!!!所有的前提是你导入了struts2-spring-plugin-2.1.8.1.jar等jar。因为Struts2和Spring都是独立的框架,整合的话,就是要两者有沟通,上述jar就是桥梁吧。

posted @ 2013-06-13 22:28  idealing  阅读(150)  评论(0编辑  收藏  举报