`

org.springframework.web.filter.CharacterEncodingFilter

 
阅读更多

来源:http://blog.sina.com.cn/s/blog_92b93d6f0100ypp9.html

 

在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter

1.看清结构:

详解Spring中的CharacterEncodingFilter【原】
可以看到其继承GenericFilterBean和OncePerRequestFilter,也就是说,这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。

 

2.官方解释:

Servlet 2.3/2.4 Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form. (这句话就说你在html页面或表单中设置编码是没有用的)

This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true").(只要你设置了foreEncoding=true,则在代码中设置编码格式没用,)In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4+ containers (although this will usually be overridden by a full content type set in the view).

 

3.如何使用

下面来看看如何在web.xml中配置:

 <filter>
  <filter-name>encodingFilter</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>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

 其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。

 

4.源码赏析

详解Spring中的CharacterEncodingFilter【原】

当Servlet容器启动的时候,会读取web.xml中对于过滤器的配置信息, 读取到<init-param>中的子标签<param-name>encoding和forceEncoding所对应的<param-value>的值,再通过调用该类setEncoding(String encoding)和setForceEncoding(boolean forceEncoding) 将值注入到这连个字段中。
详解Spring中的CharacterEncodingFilter【原】

在这里就能看到为什么设置foreEncoding为true会覆盖掉request.getCharacterEncoding()中的方法了吧,呵呵,还是那句话,源码之前了无秘密,只有深入到源代码之中才能看清本质。

 

 

分享到:
评论

相关推荐

    spring-web-2.5.jar

    org.springframework.web.filter.CharacterEncodingFilter.class org.springframework.web.filter.CommonsRequestLoggingFilter.class org.springframework.web.filter.DelegatingFilterProxy.class org.spring...

    apache-tomcat-7.0.69.zip

    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java...

    java head space.txt

    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java...

    SpringMVC Tomcat控制台乱码问题解决方案

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;characterEncodingFilter&lt;/...

    IDEASSM框架实战CRUDSSM整合配置MyBatis逆向工程.docx

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceRequestEncoding &lt;param-value&gt;true ...

    详解Spring mvc的web.xml配置说明

    例如,`org.springframework.web.filter.CharacterEncodingFilter`是一个预设的过滤器,用于确保请求和响应的字符编码一致。我们可以在`filter-mapping`标签中指定哪些URL应该经过此过滤器。 3. **处理请求...

    Spring+mybatis

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;CharacterFilter&lt;/filter-...

    Spring MVC 框架应用实例

    org.springframework.web.filter.CharacterEncodingFilter &lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-name...

    spring配置编码格式

    org.springframework.web.filter.CharacterEncodingFilter &lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;setCharacterEncoding&lt;/filter-name&gt; ...

    SSH2整合详细示例

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;characterEncodingFilter&lt;/...

    SpringMVC 学习总结

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;utf-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;CharacterEncodingFilter&lt;/...

    spring MVC所需jar包和filter的配置

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceEncoding &lt;param-value&gt;true &lt;/filter&gt; &lt;filter...

    ssh2的基于MVC的curd(带事务)

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;utf-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-...

    javaSSH框架搭建配置

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;utf-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;...

    SS整合1

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceEncoding &lt;param-value&gt;true &lt;/filter&gt; &lt;filter...

Global site tag (gtag.js) - Google Analytics