近期关于struts2与spring整合的讨论热度持续攀升,我们通过多方渠道收集整理了相关资讯 ,并进行了系统化的梳理 。若这些内容恰好能为您提供参考,将是我们最大的荣幸。
Spring整合Struts2的两种方案 收藏
一 、需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件,比如commons-logging.jar等等 ,另外还需要Struts2发布包中的struts2-spring-plugin-x.xx.jar。
二、在web.xml中增加WebApplicationContext的相应配置,以下两种配置方式本质是一样的 。
1. Servlet 2.3及以上版本可以使用监听器,相应配置如下:
<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>
如果spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录下 ,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件。若存在多个Spring配置文件,则在<param-value>中依次列出 ,之间以逗号隔开。
2. Servlet 2.3以下版本由于不支持<listener>,需要配置<servlet>,格式如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
如果spring配置文件被命名为applicationContext.xml ,并且放在WEB-INF目录下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件 ,或者是名字为contextConfigLocation的ServletContext参数所指定的文件。由于该Servlet配置只是为了在容器启动时能启动ContextLoaderServlet使其工作,而不需要引用该Servlet,所以不需要配置<servlet-mapping> 。
三、在web.xml中完成加载WebApplicationContext之后 ,接下来就可以做到Spring和Struts2的整合了。整合有两种方法,分别叙述如下:
1. 第一种实现方法:
1) 将Struts的业务逻辑控制器类配置在Spring的配置文件中,业务逻辑控制器中引用的业务类一并注入。注意,必须将业务逻辑控制器类配置为scope= ”prototype”!
示例如下:
<bean id=”LoginAction ” class=”yaso.struts.action.LoginAction”>
<property name=”loginDao ” ref=”LoginDao”/>
</bean>
2) 在struts.xml或者等效的Struts2配置文件中配置Action时 ,指定<action>的class属性为Spring配置文件中相应bean的id或者name值 。示例如下:
<action name= ”LoginAction” class=”LoginAction ”>
<result name=”success”>/index.jsp</result>
</action>
2. 第二种实现方法:
1) 业务类在Spring配置文件中配置,业务逻辑控制器类不需要配置,Struts2的Action像没有整合Spring之前一样配置 ,<action>的class属性指定业务逻辑控制器类的全限定名。
2) 业务逻辑控制器类中引用的业务类不需要自己去初始化,Struts2的Spring插件会使用bean的自动装配将业务类注入进来,其实业务逻辑控制器也不是Struts2创建的 ,而是Struts2的Spring插件创建的。默认情况下,插件使用by name的方式装配,可以通过增加Struts2常量来修改匹配方式:设置方式为:struts.objectFactory.spring.autoWire = typeName ,可选的装配参数如下:
a) name:等价于Spring配置中的autowire= ”byName”,这是缺省值 。
b) type:等价于Spring配置中的autowire=”byType ”。
c) auto:等价于Spring配置中的autowire=”autodetect”。
d) constructor:等价于Spring配置中的autowire=” constructor ” 。
四 、如果原先在Struts2中使用了多个object factory,则需要通过Struts2常量显式指定object factory ,方式如下:struts.objectFactory = spring;如果没有使用多个object factory,这一步可以省略。
五、可以通过设增加Struts2常量来指定是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true 。设置方式为:struts.objectFactory.spring.useClassCache = false。
六、至此,完成了两种方式的整合。比较这两种整合方式 ,其本质是一样的。不同之处在于,使用第二种自动装配的方式时,由于没有在Spring中配
关于struts2与spring整合的探讨就到这里 ,您是否还有其他想了解的内容?欢迎在评论区留言告诉我们,同时别忘了点击关注哦!
评论列表(3条)
我是博瑞号的签约作者“admin”
本文概览:近期关于struts2与spring整合的讨论热度持续攀升,我们通过多方渠道收集整理了相关资讯,并进行了系统化的梳理。若这些内容恰好能为您提供参考,将是我们最大的荣幸。Spri...
文章不错《struts2与spring整合》内容很有帮助