一.客户端数据post方法传到后台中文乱码问题(tomcat默认字符集ISO-8859-1,不支持中文)

解决:spring框架提供字符集编码的过滤器(CharacterEncodingFilter),解决post请求的中文乱码。在web.xml中,配置字符集编码过滤器:

<!-- 配置字符集编码过滤器 -->

  <filter>

     <filter-name>encodingFilter</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>encodingFilter</filter-name>

     <!-- 配置所有请求都经过字符集编码过滤器处理 -->

     <url-pattern>/*</url-pattern>

  </filter-mapping>

二.解决get请求数据乱码的问题

方法一:

try{    // 1.获取参数数据

         String name = queryVo.getItem().getName();

         // 2.根据ISO-8859-1获取到参数的字节码

         byte[] bytes = name.getBytes("ISO-8859-1");

         // 3.使用UTF-8重新编码参数数据

         name = new String(bytes, "UTF-8");

         // 4.重新设置参数数据

         queryVo.getItem().setName(name);

     }catch(Exception e){

         e.printStackTrace();

     }

方法二:

在pom.xml中配置Tomcat编码方式

<build>

     <finalName>ssm</finalName>

     <plugins>

         <plugin>

             <groupId>org.apache.tomcat.maven</groupId>

             <artifactId>tomcat7-maven-plugin</artifactId>

             <version>2.1</version>

             <configuration>

                 <!-- tomcat 的端口号 -->

                 <port>8080</port>

                 <!-- 访问应用的路径 -->

                 <path>/ssm</path>

                 <!-- URL按UTF-8进行编码,解决中文参数乱码 -->

                 <uriEncoding>UTF-8</uriEncoding>

                 <!-- tomcat名称 -->

                 <server>tomcat7</server>

             </configuration>

         </plugin>

     </plugins>

</build>

posted on 2018-06-24 18:55  SuperTan  阅读(155)  评论(0编辑  收藏  举报