English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method to Solve Encoding Issues When Passing Parameters in Spring MVC Shared

Summary

The issue of Chinese character encoding errors in socialism with Chinese characteristics is a problem we often encounter. There are many solutions, and this article introduces the one-time solution for Chinese character encoding errors in GET and POST methods.

Solution for Chinese character encoding errors in GET submissions

In the controller file with乱码, use the following method to convert the encoding to UTF-8

String str = new String(request.getParameter("parameterName").getBytes("iso-8859-1"), "utf-8");

Modify the server.xml file in the Tomcat server where the project is located

Modify

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"redirectPort="8443"/>

Modified to:

<Connector URIEncoding="UTF-8"connectionTimeout="20000" port="8080" protocol="HTTP/1.1"redirectPort="8443"/>

The above method can still solve the Chinese garbled code problem in the GET method of Ajax requests.

Solution to the Chinese garbled code problem in POST submission

Add the following content to the web.xml file:

<!-- Filter to solve the problem of Chinese garbled code in POST submission, note that it can only solve the problem of Chinese garbled code in POST submission -->
 <filter>
   <filter-name>CharacterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
     <param-name>encoding</param-name>
     <param-value>UTF-8</param-value>
   </init-param>
 </filter>
 <filter-mapping>
   <filter-name>CharacterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

Summary

This is the full content of this article sharing the solution to the Chinese garbled code problem of parameter passing in Spring MVC. I hope it will be helpful to everyone. Those who are interested can continue to refer to this site:

Source Code Analysis of Method Execution After Spring SpringMVC Initialization

Detailed Explanation of Implementing Session Expiration Listening with SpringMVC Interceptor

Detailed Explanation of User Query Code for Developing RESTful API with SpringMVC

Welcome to leave a message if there is anything lacking. Thank you for your friends' support to this site!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like