struts spring 配置

本文介绍了两种启动Spring框架的方法:一种是由web容器启动Spring,在web.xml中进行配置;另一种是由Struts启动Spring,在struts-config.xml中进行配置。推荐使用第一种方法,因为它允许在脱离Action的地方使用Spring。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring的启动有两种。
方法一:由web容器启动spring。
在web.xml中添加spring的配置。
<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>

此种方法对所有的web应用程序都起作用。但是有个前提,就是web容器必须支持Listener。


方法二:由struts来启动spring
在struts-config.xml中添加。
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>


spring加载后,需要在struts-config.xml中添加:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
添加这个的目的是将所有的action的管理由spring来接手。这样我们就能够对action进行注入了。


我推荐大家使用第一种方式加载spring。因为第二种方式加载的spring,不能在脱离action的地方使用。
比如,我们要在jsp页面中使用spring来获得一个bean,此时就不能得到。如果使用第一种方式加载spring,就可以在jsp中使用spring。当使用了jms、webservice等技术的时候,也是这个样子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值