對handlerexecutionchain類的深入理解
更新時間:2017年07月04日 08:36:07 投稿:jingxian
下面小編就為大家?guī)硪黄獙andlerexecutionchain類的深入理解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
HandlerExecutionChain類比較簡單,好理解。
/* * 處理器執(zhí)行鏈由處理器對象和攔截器組成。 */ public class HandlerExecutionChain {
下面是類的部分屬性。
private final Object handler; //處理器對象。 private HandlerInterceptor[] interceptors; //攔截器數(shù)組 private List<HandlerInterceptor> interceptorList; //攔截器列表
/** * Apply preHandle methods of registered interceptors. * @return {@code true} if the execution chain should proceed with the * next interceptor or the handler itself. Else, DispatcherServlet assumes * that this interceptor has already dealt with the response itself. * 執(zhí)行已經(jīng)注冊的攔截的 preHandle()方法。如果返回true,則執(zhí)行鏈可以執(zhí)行下一個攔截器的preHandle()方法或 handler 自身。 * 否則, */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }
/* * 執(zhí)行已經(jīng)注冊的攔截器 postHandle()方法。 */ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = interceptors.length - 1; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; interceptor.postHandle(request, response, this.handler, mv); } } }
/** * 這個方法只會執(zhí)行preHandle()方法已經(jīng)成功執(zhí)行并且返回true的攔截器中的postHandle()方法。 */ void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = this.interceptorIndex; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; try { interceptor.afterCompletion(request, response, this.handler, ex); } catch (Throwable ex2) { logger.error("HandlerInterceptor.afterCompletion threw exception", ex2); } } } }
以上這篇對handlerexecutionchain類的深入理解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse搭建spring開發(fā)環(huán)境圖文教程(推薦)
下面小編就為大家?guī)硪黄狤clipse搭建spring開發(fā)環(huán)境圖文教程(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Java this super代碼實例及使用方法總結(jié)
這篇文章主要介紹了Java this super代碼實例及使用方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03

SpringMVC中DispatcherServlet的HandlerMapping詳解
這篇文章主要介紹了SpringMVC中DispatcherServlet的HandlerMapping詳解,上回說的Handler,我們說是處理特定請求的,也就是說,不是所有的請求都能處理,那么問題來了,我們怎知道哪個請求是由哪個Handler處理的呢,需要的朋友可以參考下
2023-10-10