DispatchAction, LookupDispatchAction, MappingDispatchAction


1) DispatchAction就是在struts-config中用parameter參數(shù)配置一個(gè)表單字段名,這個(gè)字段的值就是最終替代execute被調(diào)用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的請求將根據(jù)parameter被分發(fā)到"save"或者"edit"或者什么。但是有一點(diǎn),save()或者edit()等方法的聲明和execute必須一模一樣。同時(shí):刪除以前的execute(----)方法,如:
-------
-------
public class UserAction extends DispatchAction

{

??? public ActionForward addUser (ActionMapping mapping,ActionForm form,

??????????? HttpServletRequest request,HttpServletResponse response) throws Exception

??? {

???????????? // 增加用戶業(yè)務(wù)的邏輯

??????????? return mapping.findForward(Constant. FORWARD_ADD );

??? }

???

??? public ActionForward delUser(ActionMapping mapping,ActionForm form,

??????????? HttpServletRequest request,HttpServletResponse response) throws Exception

??? {

???????????? // 刪除用戶業(yè)務(wù)的邏輯

??????????? return mapping.findForward(Constant. FORWARD_SUCCESS );

??? }

??? public ActionForward updateUser(ActionMapping mapping,ActionForm form,

??????????? HttpServletRequest request,HttpServletResponse response) throws Exception

??? {

???????????? // 更新用戶業(yè)務(wù)的邏輯

??????????? return mapping.findForward(Constant. FORWARD_SUCCESS );

??? }

}

?

2) LookupDispatchAction繼承DispatchAction, 用于對同一個(gè)頁面上的多個(gè)submit按鈕進(jìn)行不同的響應(yīng)。其原理是,首先用MessageResource將按鈕的文本和ResKey相關(guān)聯(lián),例如button.save=save;然后再復(fù)寫getKeyMethodMap(), 將ResKey和MethodName對應(yīng)起來, 例如map.put("button.save", "save"); 其配置方法和DispatchAction是一樣的,? parameter屬性為:method 如:
<html:submit property="method">
<bean:message key="button.save"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
注意:此Action一定要有對應(yīng)的FormBean, 就是說一定要有name屬性, 同時(shí)要?jiǎng)h除此類的execute(-----)方法
資源文件如:
button.save=save
button.delete=delete

3) MappingDispatchAction是1.2新加的, 也繼承自DispatchAction. 它實(shí)現(xiàn)的功能和上面兩個(gè)區(qū)別較大, 是通過struts-config.xml將多個(gè)action-mapping映射到同一個(gè)Action類的不同方法上, 典型的配置就是:
<action-mappings>
<action path="/saveUser" type="logic.UserAction" parameter="save"></action>
<action path="/editUser" type="logic.UserAction" parameter="edit"></action>
</action-mappings>
然后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
等方法