六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1776|回复: 0

Spring整合Struts

[复制链接]
 楼主| 发表于 2014-5-30 18:14:53 | 显示全部楼层 |阅读模式
Spring整合Struts
为什么整合?
使用Spring的IOC功能将业务类注入Action
由Spring创建并管理Action
Spring容器通过Web容器启动(配置监听器ContextLoaderListener即可完成)

步骤:
1、如何启动Spring容器?
配置监听器ContextLoaderListener即可完成,在web.xml中配置
<context-param>
  <param-name>contextConfigLocation</param-name><!-- 固定的 -->
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 配置监听器启动Spring容器 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、将创建Action的工作委托给Spring完成,该工作由Spring提供的代理类DelegatingActionProxy实现。
applicationContext.xml:
<!-- name的值必须是Action的path属性值 -->
<bean name="/test" class="com.aptech.struts.action.TestAction"></bean>

struts-config.xml:
<action path="/test" type="org.springframework.web.struts.DelegatingActionProxy" />
3、将业务对象注入到Action
4、为了能够在Action中访问WEB对象,如ServletContext,则必须将Action配置在单独的文件中,并通过Struts插件(ContextLoaderPlugIn)来加载该文件。
struts-config.xml:
<plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
   value="classpath:action-beans.xml" />
</plug-in>
得到ServletContext里的spring对象有如下方法:
ServletContext servletContext = this.getServlet().getServletContext();
方法一:
  {
   XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext) servletContext
     .getAttribute(WebApplicationContext
       .ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  }
方法二:
  {
   WebApplicationContext xmlWebApplicationContext =
    WebApplicationContextUtils
     .getWebApplicationContext(servletContext);
   System.out.println(xmlWebApplicationContext);
  }
摘自:http://blog.csdn.net/startym/article/details/3349970

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表