整合Struts与Spring的三种方式
1.在Struts 中使用WebApplicationContext 调用spring.
1)加载spring 的Bean文件。
<listener>
ContextLoaderListener
</listener>
2)访问spring 的Bean 对象。
WebapplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserBean userBean = (UserBean)ctx.getBean("userBean");
2.配置Struts 的Action 托管给Spring.
1) 在Struts-config.xml 中配置ContextLoaderPlugIn 插件,来加载spring 配置。
<set-property property ="" value="/WEB-INF/applicationContext.xml"/>
2)配置Action托管给spring.分两种方式:
(1)在struts-config.xml中配置使用spring的DelegatingRequestProcessor,
来重载struts默认的RequestProcessor.
使用DelegatingRequestProcessor
<controller>DelegatingRequestProcessor</controller>
<action path="/usr" type ="com.....UserAction"/>
<bean name="/usr" class ="com.....UserAction"/>
(2)将struts-config.xml中<action-mapping>的type 属性设为DelegatingActionProxy.
使用DelegatingActionProxy
<action path="/usr" type ="or.springframework.web.struts.DelegatingActionProxy">
<forward name="" path=""/>
</action>
<bean name ="" class="com.....UserAction"/>
3.继承Spring的ActionSupport 类。
public class UserAction extends DispatchActionSupport {
?public ActionForward execute(ActionMapping mapping,ActionForm form,
?HttpServletRequest request,HttpServletResponse response) throws Exception{
?WebApplicationContext ctx = getWebApplicationContext();
?UserBean userBean = (UserBean)ctx.getBean("userBean");
?return mapping.findForward("success");
? }
}