1.当然首先在web.xml中配好struts的ActionServlet,这就不多说了;
2.为了让struts action知道某个包含在spring上下文中的bean,我们在struts-config.xml文件中注册spring:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="ContextConfigLocation"
value/WEB-INF/config/applicationContext-service.xml, /WEB-INF. . . . . ." />
</plug-in>
不过好像在web.xml中若有以下配置也能达到同样的效果:
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3.假设这里有这样一个jsp文件:zone.jsp,也就三个输入框,两个submit键,一个“新增”,一个“更改”
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="/WEB-INF/pages/common/taglibs.jsp"%>
<html>
<body>
<html:form action="/zone" >
<table>
<tr> <td>
<html:text property="zoneId" size="20" maxlength="10"/>
</td> </tr>
<tr> <td>
<html:text property="cityId" size="20" maxlength="5"/>
</td> </tr>
<tr> <td>
<html:text property="name" size="20" maxlength="20"/>
</td> </tr>
<tr> <td>
<!-- 下面两个submit都发送请求到struts-config.xml中配置的同一个“path="/zoneSave"”的action中-->
<html:submit οnclick="this.form.action='zoneSave.xhtml'">新增</html:submit>
<html:submit property="modify" οnclick="this.form.action='zoneSave.xhtml'">更改</html:submit>
</td> </tr>
</table>
</html:form>
</body>
</html>
jsp对应一个action,他继承自自定义的XXXEventDispatchAction :
package com.xxxx.web.action;
import javax.servlet.http.HttpServletRequest;
. . . . . .
public class ZoneAction extends XXXEventDispatchAction {
//public class XXXEventDispatchAction extends EventDispatchAction
//org.apache.struts.actions.EventDispatchAction是内置action类
//XXXEventDispatchAction封装了如getBean()等一些基本方法
private ZoneManager zoneManager;
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ZoneForm zForm = (ZoneForm) form;
Zone zone = new Zone();
zone.setZoneId(zForm.getZoneId());
City city = new City();
city.setCityId(zForm.getCityId());
zone.setCity(city);
zone.setName(zForm.getName());
zoneManager.addZone(zone);
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
public ActionForward modify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ZoneForm zForm = (ZoneForm) form;
Zone zone = new Zone();
zone.setZoneId(zForm.getZoneId());
City city = new City();
city.setCityId(zForm.getCityId());
zone.setCity(city);
zone.setName(zForm.getName());
zoneManager.updateZone(zone);
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
// 向spring暴露set()方法
public void setZoneManager(ZoneManager zoneManager) {
this.zoneManager = zoneManager;
}
}
配置struts-config.xml,注册struts action:
<!-- 这里的show对应到上面action类的show()方法中-->
<action path="/zone" parameter="show, default=show"
name="zoneForm" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="false" input="/WEB-INF/pages/zone.jsp">
<forward name="success" path="/WEB-INF/pages/zone.jsp" />
</action>
<!-- 因为下面这里有“default=add”,所以 页面中的“新增”submit标签不用像“更新”标签那样指定property属性-->
<!-- 这里的add, modify分别对应到上面action类的add() 和 modify()两个方法中-->
<action path="/zoneSave" parameter="add, modify, default=add"
name="zoneForm" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="true" input="/WEB-INF/pages/zone.jsp">
<forward name="success" path="/WEB-INF/pages/zone.jsp" />
</action>
4.注意上面的DelegatingActionProxy,我们在struts-config.xml注册的实际是个代理action,现在我们需要作为spring bean来配置struts action,比如我们可能在名为applicationContext-action.xml的文件中这样配置:
<bean name="/zone" class="com.xxxx.web.action.ZoneAction"
singleton="false">
<property name="zoneManager">
<ref bean="zoneManager"/>
</property>
</bean>
<bean name="/zoneSave" class="com.xxxx.web.action.ZoneAction"
singleton="false">
<property name="zoneManager">
<ref bean="zoneManager"/>
</property>
</bean>
注意,这里Bean的全称类名com.xxxx.web.action.ZoneAction就是前面对应于那个jsp的action.而特别要注意的是,这里Bean的name属性必须和struts-config.xml中的path属性完全一致。DelegatingActionProxy会使用path属性的值在spring上下文中查找真正的action。
至此,页面中的提交请求将能正确有效地与业务中间层的zoneManager进行交流了!