网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

Spring对Struts的整合

http://www.diybl.com/ 2008-3-26  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

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" 



欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言