一、Struts 和 Spring 整合
第1步:将spring做为插件加到struts_config.xml中:
<struts-config>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
这是加载spring的context
第2步:将控制权交给Spring定制的Action处理,在 struts_config.xml的配置action的type:
<action-mappings>
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm">
<forward name="success" path="/main.jsp" />
<forward name="failure" path="/login.jsp" />
</action>
</action-mappings>
DelegatingActionProxy同样是 org.apache.struts.action.Action的一个子类,它将把调用请求转交给真正的Action实现。
第3步:在spring的配置文件applicationContext.xml中配置Action的bean
<bean name="/login" class="cn.uolee.wap.LoginAction" singleton="false">
<property name="userDAO">
<ref bean="userDAOProxy" />
</property>
</bean>
这样当用户请求login.do页面的时候, DelegatingActionProxy将在 applicationContext.xml中获取真正的Action实例,并且调用Action的execute()方法。
下面是 DelegatingActionProxy的 execute方法代码:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//获得实际的Action实例,并将请求转交
Action delegateAction = getDelegateAction(mapping);
return delegateAction.execute(mapping, form, request, response);
}