DispatchAction是Action类的子类,它可以将用户的请求分派到一个公共的方法中处理。下面通过一个例子来加以说明:
我引用的是struts1.1
一。创建MyJsp.jsp
<body>体中添加:
<a href="modify.do?method=add">add</a>
<a href="modify.do?method=update">update</a>
二。配置struts-config.xml
<action-mappings>
<action path="/modify"
parameter="method"
type="com.nalis.struts.action.ModifyAction" />
</action-mappings>
三。创建Action (要继承org.apache.struts.actions.DispatchAction)
不要在DispatchAction的子类重写execute方法,即删除掉TestCaseAction中的execute方法,添加add,update方法:
public ActionForward update(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
System.out.println("update");
return null;
}
public ActionForward add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
System.out.println("add");
return null;
}
优点:将多个简单的处理方法放置到一个Action中,简化应用程序的设计,开发和维护。