Spring中的CharacterEncodingFilter


spring的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
"http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
            http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    
    
        class>org.springframework.web.context.ContextLoaderListenerclass>
    
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

   
    
        mybatis
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            classpath:mybatis-servlet.xml
        
        1
    

    
        mybatis
        /
    
    

    
        SpringEncodingFilter
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        SpringEncodingFilter
        /*
    

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

1.看清结构:



 
 
可以看到其继承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中配置:

 

Xml代码  String encoding)和setForceEncoding(boolean forceEncoding) 将值注入到这连个字段中。


 

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

Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    6.     <filter>  
    7.         <filter-name>EncodingFilterfilter-name>  
    8.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
    9.         <init-param>  
    10.             <param-name>encodingparam-name>  
    11.             <param-value>UTF-8param-value>  
    12.         init-param>  
    13.     filter>  
    14.     <filter-mapping>  
    15.         <filter-name>EncodingFilterfilter-name>  
    16.         <url-pattern>/*url-pattern>  
    17.     filter-mapping>  
    18. web-app>