Springmvc send Ajax appear Chinese garbled solution Rollup _ajax related

Source: Internet
Author: User
Tags character set constructor

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.