spring中的字符集過濾器能夠很方便的爲咱們解決項目中出現的中文亂碼問題,並且使用方法也很簡單,只須要在web.xml文件中配置一下該過濾器,設置兩個重要的參數(encoding和forceEncoding)便可:java
<!-- 配置請求過濾器,編碼格式設爲UTF-8,避免中文亂碼--> <filter> <filter-name>springUtf8Encoding</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> <filter-mapping> <filter-name>springUtf8Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
如下是Spring字符集過濾器的源碼:web
public class CharacterEncodingFilterextends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; /** * Set the encoding to usefor requests. This encoding will be passed into a * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. * <p>Whether this encoding will overrideexisting request encodings * (and whether it will beapplied as default response encoding as well) * depends on the {@link #setForceEncoding "forceEncoding"} flag. */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Set whether theconfigured {@link #setEncoding encoding} of this filter * is supposed to overrideexisting request and response encodings. * <p>Default is "false", i.e. do notmodify the encoding if * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} * returns a non-null value.Switch this to "true" to enforce the specified * encoding in any case,applying it as default response encoding as well. * <p>Note that the response encoding will onlybe set on Servlet 2.4+ * containers, sinceServlet 2.3 did not provide a facility for setting * a default responseencoding. */ public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response,FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); } }
由源碼能夠知道,該字符集過濾器有兩個重要參數,分別是encoding和forceEncoding,這兩個參數分別有什麼做用呢?如下是參考文檔的介紹:spring
setEncoding public voidsetEncoding(Java.lang.String encoding) Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call. setForceEncoding public voidsetForceEncoding(boolean forceEncoding) Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.
經過參考文檔,咱們能夠知道:app
1.第一個方法setEncoding()至關於:ServletRequest.setCharacterEncoding(java.lang.String)
ide
2.第二個方法setForceEncoding()的做用是:強制ServletResponse的編碼格式和ServletRequest的編碼格式同樣。
this
也就是說,不管是request仍是response,encoding設置了二者的編碼格式,只不過forceEncoding默認值爲false,此時就只是設置了request的編碼格式,即在Servlet中:編碼
request.setCharacterEncoding("XXXX"); url
若是設置forceEncoding的值爲true時,至關於Servlet中:spa
request.setCharacterEncoding("XXXX");.net
response.setCharacterEncoding(「XXXX」);
如今咱們回過頭來看看最初給你們看的web.xml中那部分過濾器的配置,相信你們都明白了,配置的做用至關於Servlet中的:
@RequestMapping(value="XXXXX") public void XXXXX(User user,HttpServletRequestreq,HttpServletResponse resp) throws UnsupportedEncodingException { resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); ...... }
所以,在請求處理的過程當中咱們能夠不用考慮編碼方面的問題,上面兩句代碼能夠省略,編碼統一交給Spring過濾器去處理,咱們能夠專心處理咱們的業務邏輯代碼,這就是Spring字符集過濾器的方便之處。