Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)

时间:2023-03-09 07:32:55
Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)

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


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");