Hey guys, lately, I use the combination of Struts and Velocity frameworks to create some website, the problem is that when I tried to input UTF-8 Japanese character, say, a field name, which I putted in the value of "索", then I click submit ( using ), the data would be passed to an AddForm, which I have the String name field to handle the name field. Problem is that, the received string is some strange letter than the expected string "索", I set all the workspace to UTF-8, in velocity.property ( input.coding/outputcoding = UTF-8 ), content-type/charset = UTF-8, but it always returns strange string, I could set the name field directly with : public void setName(String name) { this.name = "索" } and the confirm Add work fine, but not with normally insert it to name field on the addForm, someone could point me out what was wrong ? Thanks for patient reading :D.
A:
I understood your problems is as follows, is this right?
- you can send and display “索” correctly on client browsers,
- but when the form is sent back to server, data is corrupted.
This is caused by mismatch between:
- encoding in which the request is encoded (UTF-8 as you said) and
- encoding by which the server decodes (ISO-8859-1 by default).
It can be solved by specifying server-side encoding (2nd of above) explicitly using CharacterEncodingFilter
of Spring Framework.
(note: Japanese frameworks such as Seasar and TERASOLUNA have similar filter and articles on the problem.)
habe
2009-12-01 15:15:43
Thanks for the reply, yeap, you got it right, data corrupted when sending back to server, I would try the ChracterEncodingFilter. Btw, I deal with Seasar once and saw that they can handle Unicode characters as well. Thanks again.
Niha Kush
2009-12-02 03:57:58
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>Yeah ! It works like a charm ! Thanks you ! Up there is config needed to make thing done :D
Niha Kush
2009-12-02 04:55:41