(转)

spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,CharacterEncodingFilter源代码如下:

  1. /*
  2. * Copyright 2002-2007 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.filter;
  17. import java.io.IOException;
  18. import javax.servlet.FilterChain;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. /**
  23. * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
  24. * requests. This is useful because current browsers typically do not set a
  25. * character encoding even if specified in the HTML page or form.
  26. *
  27. * <p>This filter can either apply its encoding if the request does not
  28. * already specify an encoding, or enforce this filter's encoding in any case
  29. * ("forceEncoding"="true"). In the latter case, the encoding will also be
  30. * applied as default response encoding on Servlet 2.4+ containers (although
  31. * this will usually be overridden by a full content type set in the view).
  32. *
  33. * @author Juergen Hoeller
  34. * @since 15.03.2004
  35. * @see #setEncoding
  36. * @see #setForceEncoding
  37. * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
  38. * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
  39. */
  40. public class CharacterEncodingFilter extends OncePerRequestFilter {
  41. private String encoding;
  42. private boolean forceEncoding = false;
  43. /**
  44. * Set the encoding to use for requests. This encoding will be passed into a
  45. * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
  46. * <p>Whether this encoding will override existing request encodings
  47. * (and whether it will be applied as default response encoding as well)
  48. * depends on the {@link #setForceEncoding "forceEncoding"} flag.
  49. */
  50. public void setEncoding(String encoding) {
  51. this.encoding = encoding;
  52. }
  53. /**
  54. * Set whether the configured {@link #setEncoding encoding} of this filter
  55. * is supposed to override existing request and response encodings.
  56. * <p>Default is "false", i.e. do not modify the encoding if
  57. * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
  58. * returns a non-null value. Switch this to "true" to enforce the specified
  59. * encoding in any case, applying it as default response encoding as well.
  60. * <p>Note that the response encoding will only be set on Servlet 2.4+
  61. * containers, since Servlet 2.3 did not provide a facility for setting
  62. * a default response encoding.
  63. */
  64. public void setForceEncoding(boolean forceEncoding) {
  65. this.forceEncoding = forceEncoding;
  66. }
  67. @Override
  68. protected void doFilterInternal(
  69. HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  70. throws ServletException, IOException {
  71. if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
  72. request.setCharacterEncoding(this.encoding);
  73. if (this.forceEncoding) {
  74. response.setCharacterEncoding(this.encoding);
  75. }
  76. }
  77. filterChain.doFilter(request, response);
  78. }
  79. }

上述代码显示,在配置字符集过滤器时可设定两个参数的值,如下:

l  encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值,如UTF-8等,相当于:

  1. request.setCharacterEncoding

l  forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的字符集是否也设置成encoding所指定的字符集,所以你可以选择设置为true或false,当值为true时,相当于

  1. request.setCharacterEncoding(“”);
  2. response.setCharacterEncoding(“”);

当值为false时,相当于:

  1. request.setCharacterEncoding(“”);

默认值为false。

示例:

  1. <filter>
  2. <filter-name>characterEncodingFilter</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. <init-param>
  9. <param-name>forceEncoding</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>characterEncodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

以上代码放置在web.xml中,相当于servlet中的:

    1. request.setCharacterEncoding("UTF-8");
    2. response.setCharacterEncoding("UTF-8");

spring MVC 乱码问题的更多相关文章

  1. Spring MVC 用post方式提交表单到Controller乱码问题,而get方式提交没有乱码问题

    在web.xml中添加一个filter,即可解决post提交到Spring MVC乱码问题 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> ...

  2. Spring MVC 解决乱码

    1. Spring 事务处理    Spring MVC乱码问题    三种处理数据库的方式        (1)jdbc(J2EE规范)        (2)Spring JDBCTemplate( ...

  3. Spring MVC 学习总结(三)——请求处理方法Action详解

    Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...

  4. MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。

    MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...

  5. 框架-Java:Spring MVC

    ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...

  6. 解决Spring MVC @ResponseBody返回中文字符串乱码问题

    spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...

  7. spring mvc form表单提交乱码

    spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...

  8. Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)

    Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...

  9. spring mvc 中文乱码 post与get的方法解决

    spring mvc表单提交中文参数乱码问题 今天测试spring mvc  ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...

随机推荐

  1. 如何在Windows系统下安装Linux虚拟机

    先安装虚拟机这个软件,然后在虚拟机里装linux. 1,准备,下载VM虚拟机,链接: http://pan.baidu.com/s/1z79oU 密码: vbap.和linux镜像文件,可以下载ubu ...

  2. 2017携程Web前端实习生招聘笔试题总结

    考察encodeURI encodeURI(), decodeURI()它们都是Global对象的方法. encodeURI()通过将某些字符的每个实例替换代表字符的UTF-8编码的一个或多个转义字符 ...

  3. 核心J2EE模式 - 截取过滤器

    核心J2EE模式 - 截取过滤器 背景 呈现层请求处理机制接收许多不同类型的请求,这些请求需要不同类型的处理.一些请求被简单转发到适当的处理程序组件,而其他请求必须在进一步处理之前进行修改,审核或未压 ...

  4. 数据库Schema两种含义~~

    1.数据库Schema有两种含义,一种是概念上的Schema,指的是一组DDL语句集,该语句集完整地描述了数据库的结构.还有一种是物理上的 Schema,指的是数据库中的一个名字空间,它包含一组表.视 ...

  5. PHP运算符与表达式

    一.概述: 在我们平时的开发中,最离不开的就是运算,在编写比较复杂的后台程序的时候,算法更是必不可少的.涉及到运算就应该了解PHP的运算符,下面我们来一起看一下PHP中常见的运算符,以及和其他语言的区 ...

  6. node-ejs-mongodb结合的项目案例-----引用mongoose和未引用mongoose模块

    本项目个人尝试了2种方法,一个是直接用mongod,一个是引用mongod里的mongoose. nodejs-ejs-mogondb- nodej+ejs模板,通过mogondb数据查询数据实现简单 ...

  7. 对象Equals相等性比较的通用实现

    最近编码的过程中,使用了对象本地内存缓存,缓存用了Dictionary<string,object>, ConcurrentDictionary<string,oject>,还 ...

  8. bzoj1798 [Ahoi2009]维护序列

    Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...

  9. final的用法

    先来看一段代码 class Car extends Vehicle {     public static void main (String[] args)     {         new  C ...

  10. godot新手中文系列教程1-打包安卓

    国内godot qq群 302924317 我也是个新手,可以进群多多交流. 我想要吐槽一下,官方文档有点陈旧,细节缺乏,所以某些时候不要相信官方文档,可以向我们可爱的群友提问,他们一定很乐意回答. ...