简体   繁体   中英

Why UTF8 character encoding doesn't work properly in my project?

I'm developing a web application in Spring Framework under Wildfly 10 application server. I use Eclipse IDE.

I experienced problems with the character encoding which I wasn't able to solve: some of the characters doesn't appear properly when I send them from server side to client side.

My test String object I use is " árvíztűrő tükörfúrógép "

  1. When I read the value from database and send it to the client side, everything appears fine.
  2. When I set the String value in the Controller as a ModelAndView object, it appears as '??rv?zt?±r?? t??k?¶rf??r??g?©p'
  3. When I send the value from client side by ajax as a POST variable and send it back to client side, it appears as 'árvízt?r? tükörfúrógép'.

I set all the .jsp files encoding UTF8: <%@page contentType="text/html" pageEncoding="UTF-8"%>

In Eclipse I set all the Maven modules text file encoding to UTF8. All the files are in UTF8.

What did I miss? What else should I set to get the String value right on client side? Should I set the character encoding in Wildfly 10 somehow?

Could somebody help me? If you need further information, please don't hesitate to ask. Thank you.

EDIT: Setting the character encoding as Maven property solved the second case. Now I only have problems with the third case:

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

After nearly 2 months of searching I was able to find solution to my problem. In addition to configure the server and Spring, I needed to add two more things:

On the web module of my Maven project I had to set the character encoding of the source: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Also when I sent JSONObject to client side, I had to set the character encoding: @RequestMapping(value = "/historyContent.do", produces = { "application/json; charset=UTF-8" })

And finally I can see what I wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM