`

struts spring 配置

阅读更多
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等技术的时候,也是这个样子。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics