You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Rural Hunter <ru...@gmail.com> on 2012/05/18 03:56:05 UTC

Charset problem of component event handler parameter

Hi,

I wrote a component which uses ajax request to trigger a component event:

new Ajax.Request(spec.url,
                 {
                     method: 'post',
                     parameters: {content: newContent}
                 });

In the event handler:
Object onContentChangeDetected(@RequestParameter("content") String content)
     {
         System.out.println(content);
         return null;
     }

Now the problem is, the Chinese character I received in the event 
handler turns into garbage characters. Both my page and tomcat are set 
to use UTF-8. The Chinese in normal form inputs and even the 
autocomplete mixin are correct. I checked the source of autocomplete 
since the behavior of my component is somewhat like that. but I didn't 
find any additional process on char encoding. Did I miss anything and 
what's the best place to re-decoding/encoding the characters?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Charset problem of component event handler parameter

Posted by Rural Hunter <ru...@gmail.com>.
ah, I found the cause. it's because of the file encoding setting used in 
tomcat for working around 
TAP5-1741(https://issues.apache.org/jira/browse/TAP5-1741). So the 
parameter is actually decoded correctly but just output in log with 
malformation. Sorry for the trouble.

于 2012/5/19 15:01, Rural Hunter 写道:
> anyone has any idea?
>
> 于 2012/5/18 22:43, Rural Hunter 写道:
>> Very very strange.
>> I tried to encode Chinese first in javascript then sent to the 
>> component event handler.
>>
>> Javascript:
>>                     new Ajax.Request(spec.url,
>>                     {
>>                         method: 'post',
>>                         parameters: {content: encodeURIComponent('测 
>> 试 ')},
>>                         contentType: 
>> "application/x-www-form-urlencoded; charset=UTF-8",
>>                     });
>>
>> Event handler:
>> Object onContentChangeDetected(@RequestParameter("content") String 
>> content)
>>     {
>>         try
>>         {
>>             System.out.println(content);
>>             System.out.println(URLDecoder.decode(content,"UTF-8"));
>>         }
>>         catch (Exception ex)
>>         {
>>             ex.printStackTrace();
>>         }
>>         return null;
>>     }
>>
>> I got this output:
>> %E6%B5%8B%E8%AF%95
>> 娴嬭瘯    //these are bad characters
>>
>> but when I test this in a separate class:
>>     String str="%E6%B5%8B%E8%AF%95";
>>     System.out.println(str);
>>     System.out.println(URLDecoder.decode(str, "UTF-8"));
>> I got this:
>> %E6%B5%8B%E8%AF%95
>> 测试    //good result
>>
>> How can this be explained?
>>
>> 于 2012/5/18 9:56, Rural Hunter 写道:
>>> Hi,
>>>
>>> I wrote a component which uses ajax request to trigger a component 
>>> event:
>>>
>>> new Ajax.Request(spec.url,
>>>                 {
>>>                     method: 'post',
>>>                     parameters: {content: newContent}
>>>                 });
>>>
>>> In the event handler:
>>> Object onContentChangeDetected(@RequestParameter("content") String 
>>> content)
>>>     {
>>>         System.out.println(content);
>>>         return null;
>>>     }
>>>
>>> Now the problem is, the Chinese character I received in the event 
>>> handler turns into garbage characters. Both my page and tomcat are 
>>> set to use UTF-8. The Chinese in normal form inputs and even the 
>>> autocomplete mixin are correct. I checked the source of autocomplete 
>>> since the behavior of my component is somewhat like that. but I 
>>> didn't find any additional process on char encoding. Did I miss 
>>> anything and what's the best place to re-decoding/encoding the 
>>> characters?
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Charset problem of component event handler parameter

Posted by Rural Hunter <ru...@gmail.com>.
anyone has any idea?

于 2012/5/18 22:43, Rural Hunter 写道:
> Very very strange.
> I tried to encode Chinese first in javascript then sent to the 
> component event handler.
>
> Javascript:
>                     new Ajax.Request(spec.url,
>                     {
>                         method: 'post',
>                         parameters: {content: encodeURIComponent('测试 
> ')},
>                         contentType: 
> "application/x-www-form-urlencoded; charset=UTF-8",
>                     });
>
> Event handler:
> Object onContentChangeDetected(@RequestParameter("content") String 
> content)
>     {
>         try
>         {
>             System.out.println(content);
>             System.out.println(URLDecoder.decode(content,"UTF-8"));
>         }
>         catch (Exception ex)
>         {
>             ex.printStackTrace();
>         }
>         return null;
>     }
>
> I got this output:
> %E6%B5%8B%E8%AF%95
> 娴嬭瘯    //these are bad characters
>
> but when I test this in a separate class:
>     String str="%E6%B5%8B%E8%AF%95";
>     System.out.println(str);
>     System.out.println(URLDecoder.decode(str, "UTF-8"));
> I got this:
> %E6%B5%8B%E8%AF%95
> 测试    //good result
>
> How can this be explained?
>
> 于 2012/5/18 9:56, Rural Hunter 写道:
>> Hi,
>>
>> I wrote a component which uses ajax request to trigger a component 
>> event:
>>
>> new Ajax.Request(spec.url,
>>                 {
>>                     method: 'post',
>>                     parameters: {content: newContent}
>>                 });
>>
>> In the event handler:
>> Object onContentChangeDetected(@RequestParameter("content") String 
>> content)
>>     {
>>         System.out.println(content);
>>         return null;
>>     }
>>
>> Now the problem is, the Chinese character I received in the event 
>> handler turns into garbage characters. Both my page and tomcat are 
>> set to use UTF-8. The Chinese in normal form inputs and even the 
>> autocomplete mixin are correct. I checked the source of autocomplete 
>> since the behavior of my component is somewhat like that. but I 
>> didn't find any additional process on char encoding. Did I miss 
>> anything and what's the best place to re-decoding/encoding the 
>> characters?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Charset problem of component event handler parameter

Posted by Rural Hunter <ru...@gmail.com>.
Very very strange.
I tried to encode Chinese first in javascript then sent to the component 
event handler.

Javascript:
                     new Ajax.Request(spec.url,
                     {
                         method: 'post',
                         parameters: {content: encodeURIComponent('测试')},
                         contentType: 
"application/x-www-form-urlencoded; charset=UTF-8",
                     });

Event handler:
Object onContentChangeDetected(@RequestParameter("content") String content)
     {
         try
         {
             System.out.println(content);
             System.out.println(URLDecoder.decode(content,"UTF-8"));
         }
         catch (Exception ex)
         {
             ex.printStackTrace();
         }
         return null;
     }

I got this output:
%E6%B5%8B%E8%AF%95
娴嬭瘯    //these are bad characters

but when I test this in a separate class:
     String str="%E6%B5%8B%E8%AF%95";
     System.out.println(str);
     System.out.println(URLDecoder.decode(str, "UTF-8"));
I got this:
%E6%B5%8B%E8%AF%95
测试    //good result

How can this be explained?

于 2012/5/18 9:56, Rural Hunter 写道:
> Hi,
>
> I wrote a component which uses ajax request to trigger a component event:
>
> new Ajax.Request(spec.url,
>                 {
>                     method: 'post',
>                     parameters: {content: newContent}
>                 });
>
> In the event handler:
> Object onContentChangeDetected(@RequestParameter("content") String 
> content)
>     {
>         System.out.println(content);
>         return null;
>     }
>
> Now the problem is, the Chinese character I received in the event 
> handler turns into garbage characters. Both my page and tomcat are set 
> to use UTF-8. The Chinese in normal form inputs and even the 
> autocomplete mixin are correct. I checked the source of autocomplete 
> since the behavior of my component is somewhat like that. but I didn't 
> find any additional process on char encoding. Did I miss anything and 
> what's the best place to re-decoding/encoding the characters?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org