Solve the garbled problem of getting request parameters

1. Chinese-type values ​​may appear in the front-end form submission, and garbled characters may appear in the console output of the background server

Note: The get and post submission methods of the Tomcat7.0 version form will have garbled characters. If the get method submits garbled characters, configure the corresponding configuration in the tomcat configuration file (you can find the specific solution by yourself)

①Here mainly explains Tomcat8.0 and above versions, when the form is submitted in the get method, there will be no garbled characters

 

 

 ② For Tomcat 8.0 and above, garbled characters will appear when the form is submitted in post mode

 

 

Solution: You need to configure spring's encoding filter in web.xml in the springMVC configuration file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<!-- 配置spring的编码过滤器   -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

After the configuration is complete, there will be no garbled characters when the form is submitted for a post request

 

Guess you like

Origin blog.csdn.net/kobe_IT/article/details/129487791