Spring 配置請求過濾器,編碼格式設為UTF-8,避免中文亂碼


 1 <!-- 配置請求過濾器,編碼格式設為UTF-8,避免中文亂碼-->
 2     <filter>
 3        <filter-name>springUtf8Encoding</filter-name>
 4        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 5        <init-param>
 6            <param-name>encoding</param-name>
 7            <param-value>UTF-8</param-value>
 8        </init-param>
 9        <init-param>
10            <param-name>forceEncoding</param-name>
11            <param-value>true</param-value>
12        </init-param> 
13     </filter>
14     <filter-mapping>
15        <filter-name>springUtf8Encoding</filter-name>
16        <url-pattern>/*</url-pattern>
17    </filter-mapping>

注解配置:

@Bean
CharacterEncodingFilter characterEncodingFilter(){
	CharacterEncodingFilter filter = new CharacterEncodingFilter();
	filter.setEncoding("UTF-8");
	filter.setForceEncoding(true);
	return filter;
}

spring源碼

 1 public class CharacterEncodingFilterextends OncePerRequestFilter {
 2  
 3     private String encoding;
 4  
 5     private boolean forceEncoding = false;
 6  
 7  
 8     /**
 9      * Set the encoding to usefor requests. This encoding will be passed into a
10      * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
11      * <p>Whether this encoding will overrideexisting request encodings
12      * (and whether it will beapplied as default response encoding as well)
13      * depends on the {@link #setForceEncoding "forceEncoding"} flag.
14      */
15     public void setEncoding(String encoding) {
16        this.encoding = encoding;
17     }
18  
19     /**
20      * Set whether theconfigured {@link #setEncoding encoding} of this filter
21      * is supposed to overrideexisting request and response encodings.
22      * <p>Default is "false", i.e. do notmodify the encoding if
23      * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
24      * returns a non-null value.Switch this to "true" to enforce the specified
25      * encoding in any case,applying it as default response encoding as well.
26      * <p>Note that the response encoding will onlybe set on Servlet 2.4+
27      * containers, sinceServlet 2.3 did not provide a facility for setting
28      * a default responseencoding.
29      */
30     public void setForceEncoding(boolean forceEncoding) {
31        this.forceEncoding = forceEncoding;
32     }
33  
34  
35     @Override
36     protected void doFilterInternal(
37            HttpServletRequest request, HttpServletResponse response,FilterChain filterChain)
38            throws ServletException, IOException {
39  
40        if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
41            request.setCharacterEncoding(this.encoding);
42            if (this.forceEncoding) {
43               response.setCharacterEncoding(this.encoding);
44            }
45        }
46        filterChain.doFilter(request, response);
47     }
48 }

該字符集過濾器有兩個重要參數,分別是encodingforceEncoding

setEncoding

public void setEncoding(java.lang.String encoding)

Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call.

setForceEncoding

public void setForceEncoding(boolean forceEncoding)

Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.

 

通過參考文檔,我們可以知道:

l.第一個方法setEncoding()相當於:ServletRequest.setCharacterEncoding(java.lang.String),即設置request編碼格式

2.第二個方法setForceEncoding()的作用是:同時設置ServletResponseServletRequest的編碼格式。

配置相當於代碼中:

resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");

在請求處理的過程中我們可以不用考慮編碼方面的問題,上面兩句代碼可以省略,編碼統一交給Spring過濾器去處理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM