Springmvc sends a summary of solutions for Chinese garbled characters in ajax, springmvcajax
Spingmvc is used to send requests through ajax in JS and return data in json format. The data obtained from the database is in the correct Chinese format and displayed on the page is incorrect ??, I have studied several solutions.
I'm using a sping-web-3.2.2, jar
Method 1:
Add produces = "text/html; charset = UTF-8" to @ RequestMapping"
@RequestMapping(value = "/configrole", method = RequestMethod.GET, produces = "text/html;charset=UTF-8") public @ResponseBody String configrole() { ...... }
Method 2:
Because the character set in StringHttpMessageConverter is set to ISO-8859-1 by default
So get the source code, modify it to the UTF-8 and package it into the spring-web-3.2.2.jar
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); .......... }
Method 3:
Modify the parameters of org. springframework. http. MediaType its constructor and add the configuration in the 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> </bean>
Method 4
The org. springframework. http. converter. StringHttpMessageConverter class is a class that processes requests or strings, and the default character set is ISO-8859-1, so garbled characters appear when the returned json contains Chinese characters.
The parent class of StringHttpMessageConverter has a List <MediaType> supportedMediaTypes attribute to store the MediaType type that StringHttpMessageConverter supports special processing. If the MediaType type to be processed is not in the supportedMediaTypes List, the default character set is used.
Solution: 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>
To process other MediaType types, you can add other value tags to the list tag.
I will introduce the issue of Chinese garbled characters when sending ajax via springmvc. I hope it will be helpful to you!
Articles you may be interested in:
- Spring mvc integrates freemarker Based on Annotation
- SpringMVC File Upload multi-File Upload instance
- Spring MVC Framework setup and Configuration Methods
- Upload File instances in Spring MVC
- SpringMVC + MyBatis declarative Transaction Management
- Solve springmvc + mybatis + mysql Chinese garbled Problem