簡(jiǎn)介 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必須一模一樣。
LookupDispatchAction繼承DispatchAction, 用于對(duì)同一個(gè)頁(yè)面上的多個(gè)submit按鈕進(jìn)行不同的響應(yīng)。
其原理是,首先用MessageResource將按鈕的文本和資源文件的key相關(guān)聯(lián),
例如button.save=保存;然后再?gòu)?fù)寫(xiě)getKeyMethodMap(), 將資源文件的key和MethodName對(duì)應(yīng)起來(lái),
例如map.put("button.save", "save"); 其配置方法和DispatchAction是一樣的, 使用時(shí)要這么寫(xiě): LookupDispatchAction的使用
1) 類編寫(xiě)規(guī)范 BaseAction繼承LookupDispatchAction,且必須實(shí)現(xiàn)方法protected Map getKeyMethodMap()。
這個(gè)方法將構(gòu)建資源key和方法名稱對(duì),放到Map里面返回。
代碼如下:
(非 Javadoc)
* @see org.apache.struts.actions.LookupDispatchAction#getKeyMethodMap() */
protected Map getKeyMethodMap()
{
Map map = new HashMap();
String pkg = this.getClass().getPackage().getName();
ResourceBundle methods = ResourceBundle.getBundle(pkg + ".LookupMethods");
Enumeration keys = methods.getKeys();
while (keys.hasMoreElements())
{ String key = (String) keys.nextElement();
?map.put(key, methods.getString(key));
?}
return map; }
2) 資源文件這個(gè)例子中,將資源key和方法名稱對(duì)放到資源文件LookupMethods.properties中。
資源文件LookupMethods.properties的內(nèi)容如下:
button.edit=edit button.delete=delete ...... 然后,
在struts的MessageResource使用的資源文件如
ApplicationResource.properties 中添加資源key的值:
button.edit=編輯 button.delete=刪除 ...... 當(dāng)然必須用ascii2native轉(zhuǎn)換成unicode。
3) 頁(yè)面編寫(xiě)然后界面中就可以使用以下方式提交: 或者 編輯
4) 配置 method屬性是指定的分發(fā)屬性,在struts-config.xml中配置。action的配置應(yīng)該加上parameter="method"來(lái)指定。
如: 配置好后,按上面所描述的方式提交,BaseAction類將分幾步執(zhí)行:
從配置中取得parameter屬性的值,這里為“method”。 再按method找到提交的屬性中取得method屬性的值,這里為“編輯”。
從MessageResource使用的資源文件中取得“編輯”對(duì)應(yīng)的key,這里為“button.edit”。
從getKeyMethodMap方法返回的Map中取得改key值對(duì)應(yīng)的方法名稱,這里為“edit”。 調(diào)用BaseAction類的方法edit。
例如
parameter="method"
而request.getParameter("method")="save",
其中"save"就是MethodName。
struts的請(qǐng)求將根據(jù)parameter被分發(fā)到"save"或者"edit"或者什么。
但是有一點(diǎn),save()或者edit()等方法的聲明和execute必須一模一樣。
LookupDispatchAction繼承DispatchAction, 用于對(duì)同一個(gè)頁(yè)面上的多個(gè)submit按鈕進(jìn)行不同的響應(yīng)。
其原理是,首先用MessageResource將按鈕的文本和資源文件的key相關(guān)聯(lián),
例如button.save=保存;然后再?gòu)?fù)寫(xiě)getKeyMethodMap(), 將資源文件的key和MethodName對(duì)應(yīng)起來(lái),
例如map.put("button.save", "save"); 其配置方法和DispatchAction是一樣的, 使用時(shí)要這么寫(xiě):
1) 類編寫(xiě)規(guī)范 BaseAction繼承LookupDispatchAction,且必須實(shí)現(xiàn)方法protected Map getKeyMethodMap()。
這個(gè)方法將構(gòu)建資源key和方法名稱對(duì),放到Map里面返回。
代碼如下:
(非 Javadoc)
* @see org.apache.struts.actions.LookupDispatchAction#getKeyMethodMap() */
protected Map getKeyMethodMap()
{
Map map = new HashMap();
String pkg = this.getClass().getPackage().getName();
ResourceBundle methods = ResourceBundle.getBundle(pkg + ".LookupMethods");
Enumeration keys = methods.getKeys();
while (keys.hasMoreElements())
{ String key = (String) keys.nextElement();
?map.put(key, methods.getString(key));
?}
return map; }
2) 資源文件這個(gè)例子中,將資源key和方法名稱對(duì)放到資源文件LookupMethods.properties中。
資源文件LookupMethods.properties的內(nèi)容如下:
button.edit=edit button.delete=delete ...... 然后,
在struts的MessageResource使用的資源文件如
ApplicationResource.properties 中添加資源key的值:
button.edit=編輯 button.delete=刪除 ...... 當(dāng)然必須用ascii2native轉(zhuǎn)換成unicode。
3) 頁(yè)面編寫(xiě)然后界面中就可以使用以下方式提交:
4) 配置 method屬性是指定的分發(fā)屬性,在struts-config.xml中配置。action的配置應(yīng)該加上parameter="method"來(lái)指定。
如:
從配置中取得parameter屬性的值,這里為“method”。 再按method找到提交的屬性中取得method屬性的值,這里為“編輯”。
從MessageResource使用的資源文件中取得“編輯”對(duì)應(yīng)的key,這里為“button.edit”。
從getKeyMethodMap方法返回的Map中取得改key值對(duì)應(yīng)的方法名稱,這里為“edit”。 調(diào)用BaseAction類的方法edit。