随着项目的发展,struts-config.xml配置文件、action、form等等都变的庞大起来,如果我们继续使用普通action,那我们就显得不明智了。建议使用DispatchAction,把相关操作集中到一个Action中,我们不用再去重新定义execute()方法。例如可以在DispatchAction中增加如下方法:
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception同时在struts-config.xml中定义类似的内容:
<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>
其实关键是增加parameter属性。
调用方法如下:
http://localhost:8080/myapp/saveSubscription.do?method=update
记得要使用method=xx,xx代表被调用DispatchAction中的方法名
备注:建议大家去看看struts api,那里有详细的说明。