选择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