当在stuts中需要在一个action中定义多个方法时,可以使用dispathAction来完成
下面是测试的一个简单的例子:
Action:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
* MyEclipse Struts
* Creation date: 04-21-2006
*
* XDoclet definition:
* @struts.action parameter="method"
*/
public class MyDispathAction extends DispatchAction {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward Add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("method", new String("add"));
return mapping.findForward("sucess");
}
public ActionForward Delete(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
request.getSession().setAttribute("method", new String("delete"));
return mapping.findForward("sucess");
}
}
stuts-config.xml:
<action
parameter="method"
path="/dispath"
type="com.test.struts.action.MyDispathAction"
validate="false" >
<forward name="sucess" path="/sucess.jsp" />
</action>
需要注意的是要将action中的execute方法删除!!!
调用:
http://127.0.0.1:8080/test/dispath.do?method=Add
http://127.0.0.1:8080/test/dispath.do?method=Delete
分别调用Action中的两种方法!!