spring 和 struts 整合
2014-03-26 14:26
225 查看
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中配
一、需要的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中配
相关文章推荐
- 转:struts+spring+hibernate 整合spring security
- Struts与Spring整合的方法
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- spring2.5整合hibernate3.0整合Struts
- Spring4第五讲学习笔记,整合Spring,Struts,Mybatis框架
- Struts+Spring+Hibernate整合入门详解
- Spring2.5+struts2.0+ibatis整合
- spring hibernate struts整合开发实例
- Struts-2 + Spring-2.5 + Hibernate-3 整合关键点
- spring hibernate struts整合开发实例
- spring与struts整合方式二
- Struts,Hibernate,Spring的整合
- Spring整合web应用和整合Struts1.x
- struts+spring+hibernate整合问题解决方法 陆续更新中....
- Spring3.x整合Struts2.3.x
- Spring与Struts1.x整合
- Struts2.1.6 + Spring2.5+Hibernate3.2整合
- Spring3.0.5+Mybatis3.0.5+Struts2.0.6整合
- 用maven整合struts+spring+hibernate之三 加入框架支持
- Spring与struts的整合方法(转)