zoukankan      html  css  js  c++  java
  • SpringMvc参数传递中乱码问题

    文章转载自: https://www.cnblogs.com/Hxinguan/p/5971779.html

    问题描述:

    当传递中文参数到controller类时,无乱是get方式还是post方式都出现乱码

    解决:

    1、保证所有的页面编码都是utf-8,包括jsp页面,浏览器编码设置和eclipse的编码设置。

    2、spingmvc给我们提供了一个编码过滤器,只需要在配置文件web.xml中加入即可。如下:

     1 <filter>
     2       <filter-name>characterEncoding</filter-name>
     3       <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
     4       <init-param>
     5           <param-name>encoding</param-name>
     6           <param-value>UTF-8</param-value>
     7       </init-param>
     8   </filter>
     9   <filter-mapping>
    10       <filter-name>characterEncoding</filter-name>
    11       <url-pattern>/*</url-pattern>
    12   </filter-mapping>

    复制代码
     1 <filter>
     2       <filter-name>characterEncoding</filter-name>
     3       <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
     4       <init-param>
     5           <param-name>encoding</param-name>
     6           <param-value>UTF-8</param-value>
     7       </init-param>
     8   </filter>
     9   <filter-mapping>
    10       <filter-name>characterEncoding</filter-name>
    11       <url-pattern>/*</url-pattern>
    12   </filter-mapping>
    复制代码

    3、以上两步有时只能解决post方式传递参数乱码问题,get方式还是出现乱码,则就需要改tomcat的配置文件了,打开tomcat的server.xml文件,找到以下行

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

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

    在上面行中插入URIEncoding="UTF-8",改成如下形式:

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

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

    这样就解决了springmvc中文参数传递乱码问题了。

  • 相关阅读:
    POJ1769 Minimizing maximizer(DP + 线段树)
    ZOJ3201 Tree of Tree(树形DP)
    POJ3613 Cow Relays(矩阵快速幂)
    POJ3635 Full Tank?(DP + Dijkstra)
    ZOJ3195 Design the city(LCA)
    POJ3368 Frequent values(RMQ线段树)
    POJ3686 The Windy's(最小费用最大流)
    HDU4871 Shortest-path tree(最短路径树 + 树的点分治)
    POJ3013 Big Christmas Tree(最短路径树)
    Gym100685G Gadget Hackwrench(倍增LCA)
  • 原文地址:https://www.cnblogs.com/huaixiaonian/p/8651010.html
Copyright © 2011-2022 走看看