解决tomcat页面跳转中文乱码问题

参考:http://blog.itpub.net/29254281/viewspace-1073278/

http://cmsblogs.com/?p=1510



乱码问题汇总:


1> 在tomcat的server.xml设置<Connector URIEncoding=”utf-8”  />, 使用utf8对URI中出现的中文进行decode,例如http://localhost:8080/test/测试.do -> http://localhost:8080/test/%E6%B5%8B%E8%AF%95.do, 这样能够找到对应的controller方法。若不设置使用默认值ISO-8859-1


2> 在tomcat的server.xml设置<Connector useBodyEncodingForURI=”true”  /> 能够解决query String的乱码问题。
useBodyEncodingForURI=true -> 使用http header中指定charset进行decode(例如:Content-Type: charset=UTF-8),若未指定,则使用默认值ISO-8859-1
useBodyEncodingForURI=false -> 使用默认值ISO-8859-1


3> 若只配置了URIEncoding=”utf-8”,则query string的编码也会被设置为utf-8,且http header中设置的charset不会重写这个编码。若同时设置了两项,则对于query string的编码,则与设置useBodyEncodingForURI=true的作用是一样的(会被http header中的charset重写)


4> 建议使用utf-8为主,可以在web.xml 中配置encoding filter来指定默认的编码为utf-8,避免乱码问题的出现。
 <filter>
    <filter-name>encoding_filter</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>encoding_filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

参考:http://blog.itpub.net/29254281/viewspace-1073278/

http://cmsblogs.com/?p=1510



乱码问题汇总:


1> 在tomcat的server.xml设置<Connector URIEncoding=”utf-8”  />, 使用utf8对URI中出现的中文进行decode,例如http://localhost:8080/test/测试.do -> http://localhost:8080/test/%E6%B5%8B%E8%AF%95.do, 这样能够找到对应的controller方法。若不设置使用默认值ISO-8859-1


2> 在tomcat的server.xml设置<Connector useBodyEncodingForURI=”true”  /> 能够解决query String的乱码问题。
useBodyEncodingForURI=true -> 使用http header中指定charset进行decode(例如:Content-Type: charset=UTF-8),若未指定,则使用默认值ISO-8859-1
useBodyEncodingForURI=false -> 使用默认值ISO-8859-1


3> 若只配置了URIEncoding=”utf-8”,则query string的编码也会被设置为utf-8,且http header中设置的charset不会重写这个编码。若同时设置了两项,则对于query string的编码,则与设置useBodyEncodingForURI=true的作用是一样的(会被http header中的charset重写)


4> 建议使用utf-8为主,可以在web.xml 中配置encoding filter来指定默认的编码为utf-8,避免乱码问题的出现。
 <filter>
    <filter-name>encoding_filter</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>encoding_filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

猜你喜欢

转载自blog.csdn.net/KeepStruggling/article/details/82533238