问题
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.
回答1:
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.)
来源:https://stackoverflow.com/questions/1824901/handle-utf-8-datastring-velocity-struts