SpringMVC JQuery Ajax Get Post request garbled solution in Tomcat

Source: Internet
Author: User

SpringMVC JQuery Ajax Get Post requests garbled in Tomcat solution SpringMVC 3.12 JQuery 1.8.2Tomcat 6.0.35 1. garbled characters are annoying. If Spring mvc @ Responsebody returns data without special processing, garbled characters will always occur, for the current 3.1.2 version. This solution solves the problem of <bean class = "org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerAdapter">
<Property name = "messageConverters">
<List>
<Bean class = "org. springframework. http. converter. ByteArrayHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. StringHttpMessageConverter">
<Property name = "supportedMediaTypes">
<List>
<Value> text/html; charset = UTF-8 </value>
</List>
</Property>
</Bean>
<Bean class = "org. springframework. http. converter. ResourceHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. xml. SourceHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. xml. XmlAwareFormHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. xml. Jaxb2RootElementHttpMessageConverter"/>
</List>
</Property>
</Bean> Note: Spring mvc internal, the default encoding only UTF-8, iso-8859-1 I like to use UTF-8, because JQuery and other send data is also UTF-8 mode, many open source components are like this, here the unified into a UTF-8. After the above configuration, the data returned from the background to the page is not garbled. 2. When JQuery ajax get requests are sent to the background, Chinese characters are garbled. The reason for this garbled relationship with Tomcat, need to specify tomcat to send data encoding format for the UTF-8, the default is the iso-8859-1. The way you specify is to add a configuration property: URIEncoding = "UTF-8" on Connector, the default configuration for the entire contact is: <Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding = "UTF-8"/> 3. If you like GBK, solving the above encoding problem is a nightmare for you, you need to use complicated filters to process Tomcat and modify some encoding methods of Spring. Even some dependent libraries may cause garbled Code such as dom4j ). At this time, the problem often becomes very difficult, but it is not hard to solve. Therefore, we recommend that you include databases and use UTF-8 encoding to simplify the process. 4. encoding is actually the data transmission format. It doesn't matter. If you like it, you can also use MP3 and MP4 formats for encoding, as long as they are decoded correctly, it is meaningless.

This article is from the "fuyan" blog. For more information, contact the author!

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.