LookupDispatchAction源代码:
public ActionForward execute(){
...
String parameter = mapping.getParameter();
String methodName = getMethodName(mapping, form, request, response, parameter);
return dispatchMethod(mapping, form, request, response, methodName);
}
首先获得mapping中配置的parameter,假设是“action”
再看getMethodName(...)
{
String keyName = request.getParameter(parameter);
if (keyName == null || keyName.length() == 0) {
return null;
}
String methodName = getLookupMapName(request, keyName, mapping);
return methodName;
}
keyName是从request获得的名字为action的按钮的value. 如果你的按钮是"增加",
那么keyName就是"增加",
再看getLookupMapName(...){
Locale userLocale = this.getLocale(request);
lookupMap = (Map) this.localeMap.get(userLocale);
if (lookupMap == null) {
lookupMap = this.initLookupMap(request, userLocale);
this.localeMap.put(userLocale, lookupMap);
}
...
// Find the key for the resource
String key = (String) lookupMap.get(keyName);
if (key == null) {
String message = messages.getMessage(
"dispatch.resource", mapping.getPath(), keyName);
throw new ServletException(message);
}
// Find the method name
String methodName = (String) keyMethodMap.get(key);
if (methodName == null) {
String message = messages.getMessage(
"dispatch.lookup", mapping.getPath(), key);
throw new ServletException(message);
}
return methodName;
}
先看上部分,getLookupMapName有一个initLookupMap(...){
...
Iterator iter = this.keyMethodMap.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String text = resources.getMessage(userLocale, key);
// Found key and haven't added to Map yet, so add the text
if ((text != null) && !lookupMap.containsKey(text)) {
lookupMap.put(text, key);
}
}
}
return lookupMap;
}
也就是说把你配置的keyMethodMap()里的key 和对应资源文件里的value掉个个,放到
lookupMap里.例如,("增加","button.add") ("更新","button.update").
好,再看 getLookupMapName下半部分
// Find the key for the resource
String key = (String) lookupMap.get(keyName);
if (key == null) {
String message = messages.getMessage(
"dispatch.resource", mapping.getPath(), keyName);
throw new ServletException(message);
}
// Find the method name
String methodName = (String) keyMethodMap.get(key);
if (methodName == null) {
String message = messages.getMessage(
"dispatch.lookup", mapping.getPath(), key);
throw new ServletException(message);
}
意思很明显了,用"增加"查找到key="button.add" , 之后再到keyMthodMap.get( "button.add")
从而得到你定义的方法.
所以,答案就出来了,
getMethodName(...)
{
String keyName = request.getParameter(parameter);
// 这里得到的汉字不是"增加"而是乱码,原因就是因为容器默认的编码是ISO-8859-01.
// 而你使用的是Utf-8 .
...
}
解决办法:
========================================
就是要转码,加Filter也是为了转码(ISO8859,utf-8),但是也只能转post方法的编码.所以要把你的
<form method="post">或者使用<html:form>.