(转)

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. 现代3D图形编程学习--opengl使用不同的缓存对象(译者添加)

    现代3D图形编程学习系列翻译地址 http://www.cnblogs.com/grass-and-moon/category/920962.html opengl使用不同的缓存对象 在设置颜色一章中 ...

  2. Webdriver API之元素定位

    Webdriver提供了8种元素定位方法:id.name.class name.tag name.link text.partial link text.xpath.css selector 一.以上 ...

  3. nginx学习笔记——http module分析

         源码:nginx 1.12.0           nginx由于其高性能.扩充性好等特点在迅速走红,越来越多的公司采用nginx作web服务器.负载均衡.waf等 工作,一些基于nginx ...

  4. bigdecimal更精确的浮点处理方式

    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位内有效数,超过16位,double可能会出现内存 ...

  5. 【web】之 jquery上传插件的Plupload的使用

    首先下载plupload->http://www.plupload.com 因为Plupload可配置参数比较多,所以这里讲解最常用的,结合jquery-ui展示的界面!如下: Plupload ...

  6. php流程管理

    流程控制即某个人发起一个流程,通过一层一层审核,通过后,完成整个流程,若有一层审核未通过,中断整个流程.即结束! 比如请假流程: 某一员工发起一个请假流程,那么这个流程的节点人员即他的上级,上上级,上 ...

  7. xmlplus 组件设计系列之五 - 选项卡

    这一章将设计一个选项卡组件,选项卡组件在手持设备上用的比较多,下面是一个示意图: 选项卡组件的分解 在具体实现之前,想像一下目标组件是如何使用的,对于设计会有莫大的帮助.通过观察,可以将选项卡组件分为 ...

  8. Python全栈之路-Day31

    1 反射 反射的精髓是通过字符串去获取对象属性的值 1.1 基于类和对象反射的属性 #!/usr/bin/env python # __Author__: "wanyongzhen" ...

  9. [Mobile Web]Web中如何分辨移动设备?(iPad、iPhone、Android)

    第一种, Javascript [javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片 var deviceAgent = navigator.use ...

  10. PTA自测-3 数组元素循环右移问题

    自测-3 数组元素循环右移问题  一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M≥0)个位置,即将A中的数据由(A0A1···A​N-1​​)变换为 ...