解决使用LookUpDispatchAction+中文Resource 抛出异常Request...does not contain handler parameter named ...

Error:

使用LookUpDispatchAction中,假设在getKeyMethodMap( )有如下代码:
map.put("homepage.manage.background", "manager");
map.put("homepage.customer.login", "customer");
resource.properties文件中,如果把homepage.customer.login这样的key的设成中文(utf-8 coding)的话,就会抛出异常:Request[/××] does not contain handler parameter named ××
而如果设成英文,则一切正常。
resource.properties已转成UTF-8编码)

 


 


Analyze:

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 .
...
}



 

Solution:

就是要转码,加Filter也是为了转码(ISO8859,utf-8),[加Filter转码的具体方法可以查看我的另外一篇文章http://blog.csdn.net/drean725/archive/2007/04/22/1574511.aspx]
但是也只能转post方法的编码.所以要把你的
<form method="post">或者使用<html:form>.

准备资源包文件,我们以创建一个中文文件为例:

将ApplicationResources.properties文件打开,另存为ApplicationResources_zh.properties,这只是一个过渡性质的文件。将文件中键/值对的值都用中文表示。更改完后的代码如下:

#Application Resource for the logon.jsp
logon.jsp.title
= 登录页
logon.jsp.page.heading
= 欢迎 世界!
logon.jsp.prompt.username
= 用户名:
logon.jsp.prompt.password
= 口令:
logon.jsp.prompt.submit
= 提交
logon.jsp.prompt.reset
= 复位

#Application Resource for the main.jsp
main.jsp.title
= 主页
main.jsp.welcome
= 欢迎:

#Application Resource for the LogonAction.java
error.missing.username
= <li><font color = " red " >没有输入用户名</font></li>
error.missing.password
= <li><font color = " red " >没有输入口令</font></li>

#Application Resource for the UserInfoBo.java
error.noMatch
= <li><font color = " red " >没有匹配的用户</font></li>

#Application Resource for the UserInfoBo.java
error.logon.invalid
= <li><font color = " red " >用户名/口令是无效的</font></li>
error.removed.user
= <li><font color = " red " >找不到该用户</font></li>
error.unexpected
= <li><font color = " red " >不可预期的错误</font></li>

 
使用native2ascii工具将上面文件中的中文字符转换为ascii码,并生成一个最终使用的资源文件ApplicationResources_zh_CN.properties。

具体做法是打开一个dos窗口,到mystruts/WEB-INF/classes目录下,运行如下语句:

native2ascii -encoding GBK ApplicationResources_zh.properties ApplicationResources_zh_CN.properties

生成的文件ApplicationResources_zh_CN.properties的内容如下:

#Application Resource for the logon.jsp
logon.jsp.title=/u767b/u5f55/u9875
logon.jsp.page.heading=/u6b22/u8fce /u4e16/u754c!
logon.jsp.prompt.username=/u7528/u6237/u540d:
logon.jsp.prompt.password=/u53e3/u4ee4:
logon.jsp.prompt.submit=/u63d0/u4ea4
logon.jsp.prompt.reset=/u590d/u4f4d

#Application Resource for the main.jsp
main.jsp.title=/u4e3b/u9875
main.jsp.welcome=/u6b22/u8fce:

#Application Resource for the LogonAction.java
error.missing.username=<li><font color="red">/u6ca1/u6709/u8f93/u5165/u7528/u6237/u540d</font></li>
error.missing.password=<li><font color="red">/u6ca1/u6709/u8f93/u5165/u53e3/u4ee4</font></li>

#Application Resource for the UserInfoBo.java
error.noMatch=<li><font color="red">/u6ca1/u6709/u5339/u914d/u7684/u7528/u6237</font></li>

#Application Resource for the UserInfoBo.java
error.logon.invalid=<li><font color="red">/u7528/u6237/u540d//u53e3/u4ee4/u662f/u65e0/u6548/u7684</font></li>
error.removed.user=<li><font color="red">/u627e/u4e0d/u5230/u8be5/u7528/u6237</font></li>
error.unexpected=<li><font color="red">/u4e0d/u53ef/u9884/u671f/u7684/u9519/u8bef</font></li>


从这里可以看出,所有的中文字都转换成了对应的Unicode码。

现在,再运行登录例子程序,您会发现它已经是显示的中文了。在浏览器的"工具"--"Internet选项"的"语言首选项"对话框中,去掉"中文(中国)"加上英文,再试登录程序,此时,又会显示英文。这就是说不同国家(地区)的客户都可以看到自己语言的内容,这就实现了国际化编程的基本要求。如果还要显示其他语言,可采用类似处理中文的方法进行,这里就不细讲了。

GOOD  LUCK!
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值