DispatchAction, LookupDispatchAction, MappingDispatchAction的深入分析.我們來(lái)看一下它們3者的關(guān)系.
org.apache.struts.action.Action, --父類
org.apache.struts.actions.DispatchAction, 子類
org.apache.struts.actions.LookupDispatchAction, 子類
org.apache.struts.actions.MappingDispatchAction, 子類
DispatchAction, LookupDispatchAction, MappingDispatchAction都是繼承的Action類.
DispatchAction
定義
public abstract class DispatchAction extends Action
這是一個(gè)抽象的Action,它會(huì)根據(jù)request 中的parameter來(lái)執(zhí)行相應(yīng)的方法。通個(gè)這個(gè)Action類可以將不同的Action集中到一個(gè)Action文件中來(lái)。
Struts-config.xml:
<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>
在Action中要有相應(yīng)的方法:
Public class demoAction extends 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
}
你就可以通過(guò)這樣的方法來(lái)訪問(wèn)你的程序:
http://localhost:8080/myapp/saveSubscription.do?method=update
如果parameter中參數(shù)為空,則執(zhí)行Action中unspecified方法
LookupDispatchAction
public abstract class LookupDispatchAction extends DispatchAction
通過(guò)這個(gè)Action抽象類繼承DispatchAction,它的相應(yīng)方法的執(zhí)行由 ActionMapping中parameter屬性決定。它適合在一個(gè)form中有很多按鈕,按不同的按鈕則執(zhí)行不同的操作。
struts-config.xml:
<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
ApplicationResources.properties:
button.add=Add Record
button.delete=Delete Record
JSP:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
在Action 中必須實(shí)現(xiàn)getKeyMethodMap:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
MappingDispatchAction
public class MappingDispatchAction extends DispatchAction
它的相應(yīng)方法的執(zhí)行由 ActionMapping中parameter名決定,注意這里和LookupDispatchAction不同,LookupDispatchAction的相應(yīng)方法的執(zhí)行由 ActionMapping中parameter屬性決定,
struts-config.xml:
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="method"/>
Action:
public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
for which you would create corresponding <action> configurations that reference this class:
<action path="/createSubscription"
type="org.example.SubscriptionAction"
parameter="create">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/editSubscription"
type="org.example.SubscriptionAction"
parameter="edit">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
parameter="save"
name="subscriptionForm"
validate="true"
input="/editSubscription.jsp"
scope="request">
<forward name="success" path="/savedSubscription.jsp"/>
</action>
<action path="/deleteSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="delete">
<forward name="success" path="/deletedSubscription.jsp"/>
</action>
<action path="/listSubscriptions"
type="org.example.SubscriptionAction"
parameter="list">
<forward name="success" path="/subscriptionList.jsp"/>
</action>
DispatchAction,LookupDispatchAction,MappingDispatchAction
1) DispatchAction就是在struts-config中用parameter參數(shù)配置一個(gè)表單字段名,這個(gè)字段的值就是最終替代execute被調(diào)用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的請(qǐng)求將根據(jù)parameter被分發(fā)到"save"或者"edit"或者什么。但是有一點(diǎn),save()或者edit()等方法的聲明和execute必須一模一樣。
2) LookupDispatchAction繼承DispatchAction, 用于對(duì)同一個(gè)頁(yè)面上的多個(gè)submit按鈕進(jìn)行不同的響應(yīng)。其原理是,首先用MessageResource將按鈕的文本和ResKey相關(guān)聯(lián),例如button.save=保存;然后再?gòu)?fù)寫getKeyMethodMap(), 將ResKey和MethodName對(duì)應(yīng)起來(lái), 例如map.put("button.save", "save"); 其配置方法和DispatchAction是一樣的, 使用時(shí)要這么寫:
3) MappingDispatchAction是1.2新加的, 也繼承自DispatchAction. 它實(shí)現(xiàn)的功能和上面兩個(gè)區(qū)別較大, 是通過(guò)struts-config.xml將多個(gè)action-mapping映射到同一個(gè)Action類的不同方法上, 典型的配置就是:
然后UserAction繼承MappingDispatchAction,其中有:
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
等方法
可以看到, 不管怎么變化, 其實(shí)這些類都是把execute給分解開(kāi), 不管是save, edit還是其他什么方法, 其實(shí)都是和原來(lái)的execute是等價(jià)的, save和edit之間沒(méi)有任何直接的關(guān)系, 而事實(shí)呢,它們是同一個(gè)業(yè)務(wù)模型的兩種不同操作。 我覺(jué)得這就是一個(gè)問(wèn)題,對(duì)于save和edit這兩種請(qǐng)求, 我后臺(tái)邏輯有可能只是調(diào)用service的方法那一句不一樣,其他代碼是完全一致的(例如錯(cuò)誤處理, 日志記錄等)。因此我想出了這個(gè)小東西,在execute方法內(nèi)部進(jìn)行局部分解