Use Spingmvc, in JS inside through Ajax to send requests, and return JSON format data, from the database is the correct Chinese format, display on the page is wrong, studied, there are several solutions.
Method One:
Add produces = "text/html;charset=utf-8" in @requestmapping
Java code
- @RequestMapping (value = "/configrole", method = Requestmethod.get, produces = "Text/html;charset=utf-8 ")
- Public @ResponseBody String configrole () {
- ......
- }
Method Two:
Because the character set is iso-8859-1 by default in Stringhttpmessageconverter.
So get the source code, change it into UTF-8 and pack it into Spring-web-3.2.2.jar
Java code
- Public class stringhttpmessageconverter extends abstracthttpmessageconverter< String>
- {
- Public static final Charset default_charset = charset.forname ( "UTF-8");
- ..........
- }
Method Three:
Modify the parameters of Org.springframework.http.MediaType to its constructor, and add the configuration in Applicationcontext-mvc.xml
Java code
- Public mediatype (String type, String subtype, Charset Charset) {
- Super (type, subtype, CharSet);
- }
XML code
- < Bean id="stringhttpmessageconverter"
- class = "Org.springframework.http.converter.StringHttpMessageConverter" >
- < Property name="supportedmediatypes">
- < List >
- < Bean class="org.springframework.http.MediaType">
- < Constructor-arg value="text" />
- < Constructor-arg value="plain" />
- < Constructor-arg value="UTF-8" />
- </ Bean >
- </ List >
- </ Property >
- </ Bean >
Method Four:
Set the property Defaultcharset inside the Org.springframework.http.converter.StringHttpMessageConverter directly to Utf-8
XML code
- < Bean id="stringhttpmessageconverter" class= "Org.springframework.http.converter.StringHttpMessageConverter" >
- < Property name="Defaultcharset" value="UTF-8" />
- </ Bean >
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Some solutions to the Chinese garbled characters of Spring MVC Ajax