Spring对Struts的整合
2008-05-06 13:13
85 查看
Spring 容器自动加载的方式:
一、MVC框架负责创建ApplicationContext实例,并在MVC加载时自动创建Spring容器。Struts采用的这种机制。
二、web.xml文件中加载Spring容器。Spring自己的MVC就是采用的这种策略。
对于在web.xml中加载ApplicationContext有两种策略:
一、利用ServletContextListener实现
<!-- 加载多个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
二、采用load-on-startup Servlet实现
<!-- 加载多个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml</param-value>
</context-param>
<!--采用load-on-startup Servlet实现 -->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
MVC框架负责创建ApplicationContext实例,并在MVC加载时自动创建Spring容器。由Spring IoC容器管理Action有两种方式(在struts-config.xml里配置):
一、使用DelegationRequestProcessor
在ActionServlet处将请求转发给ApplicationContext的bean
<!--加载多个配置文件-->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml"/>
</plug-in>
<!--使用spring的RequestProcessor替换struts原有的RequestProcessor-->
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
因为Struts会将拦截到的用户请求转发到Spring context下的bean,根据bean的name属性来匹配,所以无须配置type属性
<action path="/bookOperator"
type="com.ssh.book.struts.action.BookOperatorAction"
/>
<action path="/bookOperator" />
<!--Spring context下的bean-->
<bean name="/bookOperator" class="com.ssh.book.struts.action.BookOperatorAction" singleton="false">
<property name="bookBiz">
<ref bean="bookInfoBiz"/>
</property>
</bean>
因为每次请求时,都应启动新的action来处理用户请求,因此将action bean配置成非单例
因为是setter注入,所以还应配置setter方法
二、使用DelegationActionProxy,在Action处将请求转发给ApplicationContext的bean
只要去掉<controller>配置元素,必将所有的Action的实现类改为type="org.springframework.web.struts.DelegatingActionProxy"
一、MVC框架负责创建ApplicationContext实例,并在MVC加载时自动创建Spring容器。Struts采用的这种机制。
二、web.xml文件中加载Spring容器。Spring自己的MVC就是采用的这种策略。
对于在web.xml中加载ApplicationContext有两种策略:
一、利用ServletContextListener实现
<!-- 加载多个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
二、采用load-on-startup Servlet实现
<!-- 加载多个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml</param-value>
</context-param>
<!--采用load-on-startup Servlet实现 -->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
MVC框架负责创建ApplicationContext实例,并在MVC加载时自动创建Spring容器。由Spring IoC容器管理Action有两种方式(在struts-config.xml里配置):
一、使用DelegationRequestProcessor
在ActionServlet处将请求转发给ApplicationContext的bean
<!--加载多个配置文件-->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,/WEB-INF/bean.xml,/WEB-INF/action-servlet.xml"/>
</plug-in>
<!--使用spring的RequestProcessor替换struts原有的RequestProcessor-->
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
因为Struts会将拦截到的用户请求转发到Spring context下的bean,根据bean的name属性来匹配,所以无须配置type属性
<action path="/bookOperator"
type="com.ssh.book.struts.action.BookOperatorAction"
/>
<action path="/bookOperator" />
<!--Spring context下的bean-->
<bean name="/bookOperator" class="com.ssh.book.struts.action.BookOperatorAction" singleton="false">
<property name="bookBiz">
<ref bean="bookInfoBiz"/>
</property>
</bean>
因为每次请求时,都应启动新的action来处理用户请求,因此将action bean配置成非单例
因为是setter注入,所以还应配置setter方法
二、使用DelegationActionProxy,在Action处将请求转发给ApplicationContext的bean
只要去掉<controller>配置元素,必将所有的Action的实现类改为type="org.springframework.web.struts.DelegatingActionProxy"
相关文章推荐
- SSI(Struts,Spring,Ibatis)整合总结
- Struts + Spring +ibatis 整合开发步骤
- struts2+spring整合框架
- ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤
- Spring中的事务管理,hibernate整合,struts整合(佟刚)
- SSH-spring3.2与struts2.0整合小例
- Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板
- Spring 和struts 整合的三种方式
- myeclipse整合Struts1.2+Hibernate3.2+Spring2.5
- 基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
- 用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式
- 整合Spring2.5+Struts2.1+Hibernate3.2
- spring2.5整合struts2.1.8
- 用Spring更好地处理Struts动作三种整合
- idea整合struts和spring和hibernate时候的导jar错误:Unable to load configuration. - action -
- struts2——整合spring与struts2
- Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
- Spring 和struts 整合的三种方式
- 12月1日struts、spring、hibernate等框架的整合培训日记
- 使用Struts,spring和HibernateXML方式进行整合