過濾器filter和攔截器interceptor的使用區別


過濾器filter和攔截器interceptor的使用區別:

 1 <!-- 通過過濾器Filter解決spring mvc中亂碼問題 -->
 2 <filter>
 3     <filter-name>dsfasf</filter-name>
 4     <filter-class>org.springframework.web.filter.ChracterEncoding</filter-class>
 5 </filter>
 6 
 7 <filter-mapping>
 8     <filter-name>sadfdsaf</filter-name>
 9     <url-pattern>*</url-partern>
10 </filter-mapping>

通過攔截器實現:

只有一個攔截器的執行順序:preHandler -> controller -> postHandle -> afterCompletion
多個攔截器的執行順序:(場景記憶法:高速上的兩個路口的收費站)
preHandle1 (收費站1)
preHandler2 (收費站2)
controller (目的地)
postHandler2 (收費站2)
postHandler1 (收費站1)
afterCompletion2 (收費站2)
afterCompletion1 (收費站1)

1、創建攔截器類並且實現HandlerInterceptor:

public class TestInterceptor implements HandlerInterceptor{
    
    /**
     * 表示是否要把當前請求攔截下來,
     * false,表示請求將被終止,不再進入controller方法中,
     * true,表示繼續往下進行。
     * @param  表示攔截器目標對象(這里就是TestInterceptor實例化對象)
     * @return [description]
     */
    @Override
    public boolean preHandle(request, response, object){}

    /**
     * 這里可以對controller返回的結果進行修改等操作
     * @param  request      [description]
     * @param  response     [description]
     * @param  object       [description]
     * @param  modelAndView [description]
     * @return              [description]
     */
    @Override
    public postHandle(request,response, object, modelAndView){}

    /**
     * 通常應用於Controller執行結束后,資源等銷毀操作。(這個不是經常用)
     * @param  request   [description]
     * @param  response  [description]
     * @param  ojbect    [description]
     * @param  Exception [description]
     * @return           [description]
     * @throws Exception [description]
     */
    @Override
    public afterCompletion(request,response, ojbect, Exception) throw Exception{}
}

2、將攔截器注冊到springmvc容器中

<mvc:interceptors>
    <mvc:interceptor>
        <bean class="TestInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

3、配置攔截器的攔截規則

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/"></mvc:mapping>
        <bean class="TestInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

了解內容。攔截器其它的實現方式:

實現WebRequestInterceptor(org.springframework.web.context.request.攔截器其它的實現方式:實現WebRequestInterceptor)

與HandlerInterceptor區別:
1、preHandle()方法沒有返回值,意味着請求將不能終止。
2、接口的方法中參數類型不一樣。只有WebRequest

攔截器的使用場景:
場景1:處理亂碼問題. 

/**
 * 在攔截器中設置請求參數的編碼
 * @param  args [description]
 * @return      [description]
 */
boolean preHandle(HttpServerletRequest args){
    args.setCharacterEncoding("utf-8");
    return true;
}

場景2:權限驗證。比如:對用戶登錄進行判斷。

/**
 * 在攔截器中設置請求參數的編碼
 * @param  args [description]
 * @return      [description]
 */
boolean preHandle(HttpServerletRequest args){
    args.setCharacterEncoding("utf-8");

    //因為用戶登錄信息存儲在session中
    if(ags.getSession().getAttribute("user") == null){
        //用戶沒有登錄,終止請求,返回到登錄頁面
        args.getRequestDispatcher().forward(request,response);
        return false;
    }
    return true;
}


免責聲明!

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



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