Handle UTF-8 DataString ( Velocity/Struts )

2019-08-30 06:16发布

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.

标签: struts
1条回答
甜甜的少女心
2楼-- · 2019-08-30 06:22

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

查看更多
登录 后发表回答