Spring与Struts的整合
1.在struts-config.xml中加入ContextLoaderPlugIn
ContextLoaderPlunIn是Struts 1.1+ 的插件,用来为 Struts 的 ActionServlet 加载 Spring Context文件。这个Context引用 WebApplicationContext (由 ContextLoaderListener 加载) 作为它的父Context。默认的context文件是映射的 ActionServlet 的名字,加上 -servlet.xml后缀。 如果 ActionServlet 在 web.xml 里面的定义是 action, 那么默认的文件就是 /WEB-INF/action-servlet.xml。
格式如下:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
2.在Struts-config.xml中加入DelegatingRequestProcessor
用Spring的DelegatingRequestProcessor重载Struts 默认的 RequestProcessor。这样当收到一个针对Action的请求时,DelegatingRequestProcessor会自动从Spring Context中查找对应的Action Bean。
格式如下:
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
3.建立struts-config.xml与action-servlet.xml中Action与Bean的对应关系
连接 struts-config.xml 和 action-servlet.xml 中的 Action与Bean的桥梁是action 的"path"和 bean 的"name"。如果你在 struts-config.xml 文件中有如下配置:
<action path="/users" .../>
你必须在 action-servlet.xml 中将 Action bean 的名字定义为 "/users":
<bean name="/users" class="..."/>
设定完以后,action的type属性可以省略不写,因为所有的Action都在Spring Context中寻找。注意这里bean用的是name属性,而不是id,因为"/"在id属性中是非法字符。
如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为<action path="/user"/> ,而且它的 module 前缀为"admin", 那么它应该对应名为 <bean name="/admin/user"/>的bean。