SpringMVC garbled solution

I have been looking for the Springmvc garbled problem for a long time today. I have tried several methods and have not solved it. Finally, I saw an article that made me wake up and the problem was solved immediately.

Solve the garbled problem of post requests in Springmvc:

Add a filter to the web.xml file with the following content:

<filter>
        <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

The way to solve the get request: put the tomcat server.xml file in the

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

 change into

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

The most critical point is here: if you change the server.xml configuration file of the tomcat installation directory, then when you run the project with eclipse, you will find that the configuration does not work. In fact, it is because eclipse uses the configuration in eclipse when running the project. tomcat, then the problem is solved, open the tomcat configuration file in eclipse and change it to the following: see attachment

Note : After configuring useBodyEncodingForURI = "true" , the Chinese garbled problem of ordinary get requests can be solved, but the Chinese will still be garbled for get requests initiated through ajax, please change useBodyEncodingForURI = "true" to URI Encoding = "UTF-8" That's it.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327064300&siteId=291194637