无论使用哪种方式整合,都需要为 Struts装载 Spring 应用上下文环境。有以下三种方式: 1)
在struts-config.xml中使用Struts Plugin
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</plug-in>
2) 在web.xml中使用ContextLoaderListener
<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>
3) 在web.xml中使用ContextLoaderServlet
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class> org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
注意: 用Struts
PlugIn的方式加载Spring配置文件有可能导致DWR无法取得Spring中定义的bean,因为DWR有可能先于Struts被访问使用,而此时Struts配置文件还未加载!
因此,在Spring、Struts和DWR
集成时,应该在web.xml中通过ContextLoaderLisenter或ContextLoaderServlet加载Spring配置文件。
最佳实践是使用Struts PlugIn的方式加载StrutsAction配置文件/WEB-INF/action-servlet.xml,
而使用ContextLoaderLisenter或ContextLoaderServlet方式加载Spring配置文件applicationContext.xml,通过两次加载完成Spring所有配置文件的加载。
Struts1和Spring整合三种启动Spring容器的方式
最新推荐文章于 2019-08-13 15:41:00 发布