Spring整合Struts

http://blog.csdn.net/startym/article/details/3349970

 

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值