日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring mvc字符编码过滤器 CharacterEncodingFilter ,添加例外url

發(fā)布時(shí)間:2024/9/19 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring mvc字符编码过滤器 CharacterEncodingFilter ,添加例外url 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

  • spring 4.3.4.RELEASE
  • CharacterEncodingFilter : Spring MVC 提供的字符集過濾器,用于處理項(xiàng)目中的亂碼問題
  • 項(xiàng)目比較老,大部分url使用的是GBK編碼,少量url使用UTF-8編碼。
  • 需要對(duì) CharacterEncodingFilter 設(shè)置例外。即,少量url不要進(jìn)行 CharacterEncodingFilter 過濾。

CharacterEncodingFilter設(shè)置

<filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

url-pattern很霸氣,過濾所有。
forceEncoding更霸氣,強(qiáng)制編碼格式為GBK。
為什么這么設(shè)置不說了(很顯然這樣讓charset為UTF-8的JS很受傷),就說怎么辦吧。

受傷的JS

<script type="text/javascript" language="javascript" src="../codebase/laydate/laydate.js" charset="UTF-8"></script>

CharacterEncodingFilter添加例外

方法1(推薦):filter-mapping添加例外

嗯~,filter-mapping添加例外是個(gè)好辦法。

<!-- 解決編碼問題過濾器 --><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- CharacterEncodingFilter過濾器添加例外 --><filter><filter-name>characterEncodingFilter_utf8</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>characterEncodingFilter_utf8</filter-name><url-pattern>/codebase/laydate/laydate.js</url-pattern></filter-mapping>

方法2:告知CharacterEncodingFilter不用再處理了

根據(jù)CharacterEncodingFilter代碼顯示,在request的Attribute中,增加名為filter-name.FILTERED,值為TRUE的Attribute時(shí),會(huì)跳過過濾器。
本文中需要增加名為characterEncodingFilter.FILTERED的Attribute。
增加名為characterEncodingFilter.FILTERED的Attribute的方法則是:在characterEncodingFilter執(zhí)行前,再增加一個(gè)Filter。該Filter向request增加名為characterEncodingFilter.FILTERED的Attribute。(該Filter需要自己寫,此處暫不提供)

CharacterEncodingFilter的相關(guān)代碼如下:

CharacterEncodingFilter的相關(guān)代碼:

public class CharacterEncodingFilter extends OncePerRequestFilter {... }

OncePerRequestFilter的相關(guān)代碼:

public abstract class OncePerRequestFilter extends GenericFilterBean {/*** Suffix that gets appended to the filter name for the* "already filtered" request attribute.* @see #getAlreadyFilteredAttributeName*/public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED";/*** This {@code doFilter} implementation stores a request attribute for* "already filtered", proceeding without filtering again if the* attribute is already there.* @see #getAlreadyFilteredAttributeName* @see #shouldNotFilter* @see #doFilterInternal*/@Overridepublic final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws ServletException, IOException {if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {throw new ServletException("OncePerRequestFilter just supports HTTP requests");}HttpServletRequest httpRequest = (HttpServletRequest) request;HttpServletResponse httpResponse = (HttpServletResponse) response;String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {// Proceed without invoking this filter...filterChain.doFilter(request, response);}else {// Do invoke this filter...request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);try {doFilterInternal(httpRequest, httpResponse, filterChain);}finally {// Remove the "already filtered" request attribute for this request.request.removeAttribute(alreadyFilteredAttributeName);}}}/*** Return the name of the request attribute that identifies that a request* is already filtered.* <p>The default implementation takes the configured name of the concrete filter* instance and appends ".FILTERED". If the filter is not fully initialized,* it falls back to its class name.* @see #getFilterName* @see #ALREADY_FILTERED_SUFFIX*/protected String getAlreadyFilteredAttributeName() {String name = getFilterName();if (name == null) {name = getClass().getName();}return name + ALREADY_FILTERED_SUFFIX;}... }

GenericFilterBean的相關(guān)代碼:

public abstract class GenericFilterBean implements Filter, BeanNameAware, EnvironmentAware,EnvironmentCapable, ServletContextAware, InitializingBean, DisposableBean {/*** Make the name of this filter available to subclasses.* Analogous to GenericServlet's {@code getServletName()}.* <p>Takes the FilterConfig's filter name by default.* If initialized as bean in a Spring application context,* it falls back to the bean name as defined in the bean factory.* @return the filter name, or {@code null} if none available* @see javax.servlet.GenericServlet#getServletName()* @see javax.servlet.FilterConfig#getFilterName()* @see #setBeanName*/@Nullableprotected String getFilterName() {return (this.filterConfig != null ? this.filterConfig.getFilterName() : this.beanName);}... } 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的spring mvc字符编码过滤器 CharacterEncodingFilter ,添加例外url的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。