選擇dispatch方法的一個方便的方式是將它們連接到按鈕,這對於本地化應用來說是個問題,因爲按鈕的標籤可能根據用戶的場所來改變,如英文版本顯示Delete按紐而對中文用戶要顯示“刪除”
LookupDispatchAction(org.apache.struts.actions.LookupDispatchAction)通過將標籤映射到資源文件的消息關鍵字來解決這個問題,但消息關鍵字不是相應Java方法的名稱,開發人員就提供一個hash表來映射消息關鍵字和dispatch方法名(通過getKeyMethodMap方法實現)
protected Map getKeyMethodMap(ActionMapping mapping, ActionForm form,
HttpServletRequest Request) {
Map map = new HashMap();
map.put("button.add", "cerate");//把button.add映射到create方法
map.put("button.view", "read");
map.put("button.update", "update");
map.put("button.delete", "delete");
return map;
}
public ActionForward create(ActionMapping mapping,
ActionForm form,
HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException;
public ActionForward read(ActionMapping mapping,
ActionForm form,
HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException;
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException;
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException;
在JSP中按鈕要這樣來創建:
<html:form action="/dataRecord">
<html:submit property="method">
<bean:message key="button.add">
</html:submit>
<html:submit property="method">
<bean:message key="button.view">
</html:submit>
<html:submit property="method">
<bean:message key="button.update">
</html:submit>
<html:submit property="method">
<bean:message key="button.delete">
</html:submit>
</html:form>
對應的資源文件
中文ApplicationResources_zh_CN.properties
button.add=增加
button.view=查看
button.update=更新
button.delete=刪除
英文ApplicationResources_zh.properties
button.add=Add
button.view=View
button.update=Update
button.delete=Delete
原文:http://www.rjpx.net/java-peixun/891.html