解决获取请求参数的乱码问题

1.在前端表单提交中可能会出现中文类型的值,在后台服务器的控制台输出可能会出现乱码问题

注意:Tomcat7.0的版本表单的get与post提交方式都会出现乱码问题,get方式提交出现乱码问题,在tomcat配置文件中进行相应的配置(具体解决方案大家自行查找)

①此处主要说明Tomcat8.0及以上版本,表单进行get方式提交时不会出现乱码问题

 

 

 ②Tomcat8.0及以上版本,表单进行post方式提交时会出现乱码问题

 

 

解决方案:需要在springMVC配置文件中web.xml配置spring的编码过滤器

<?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>

配置完成后,在表单提交进行post请求时不会出现乱码问题

猜你喜欢

转载自blog.csdn.net/kobe_IT/article/details/129487791