Use Spingmvc, in JS inside through Ajax to send a request, and return the JSON format data, from the database is the correct Chinese format, displayed on the page is wrong?? , studied, there are several solutions.
I'm using a sping-web-3.2.2,jar.
Method One:
Join produces = "text/html;charset=utf-8" in @requestmapping
@RequestMapping (value = "/configrole", method = Requestmethod.get, produces = "text/html;charset=utf-8") Public
@ Responsebody String Configrole () {
...
Method Two:
Because the character set in the Stringhttpmessageconverter is set by default Iso-8859-1
So get the source code, modify it into UTF-8 and pack it into Spring-web-3.2.2.jar.
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's constructor method and add the configuration in Applicationcontext-mvc.xml
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>
Method 4
The Org.springframework.http.converter.StringHttpMessageConverter class is a class that processes requests or corresponding strings, and the default character set is Iso-8859-1, so garbled characters appear when there is Chinese in the return JSON.
Stringhttpmessageconverter has a list<mediatype> supportedmediatypes attribute in its parent class, The mediatype type used to store Stringhttpmessageconverter support requires special handling, and if the type of mediatype to be handled is not in the Supportedmediatypes list, the default character set is used.
Workaround, simply add the following code to the configuration file:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class= " Org.springframework.http.converter.StringHttpMessageConverter ">
<property name=" Supportedmediatypes " >
<list>
<value>application/json;charset=UTF-8</value>
</list>
< /property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
If you need to handle other mediatype types, add additional value tags to the list label
About SPRINGMVC send Ajax appeared in Chinese garbled problem small series to introduce to everyone here, hope to help everyone!