對(duì)handlerexecutionchain類的深入理解
更新時(shí)間:2017年07月04日 08:36:07 投稿:jingxian
下面小編就為大家?guī)硪黄獙?duì)handlerexecutionchain類的深入理解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
HandlerExecutionChain類比較簡(jiǎn)單,好理解。
/*
* 處理器執(zhí)行鏈由處理器對(duì)象和攔截器組成。
*/
public class HandlerExecutionChain {
下面是類的部分屬性。
private final Object handler; //處理器對(duì)象。 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)注冊(cè)的攔截的 preHandle()方法。如果返回true,則執(zhí)行鏈可以執(zhí)行下一個(gè)攔截器的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)注冊(cè)的攔截器 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);
}
}
}
/**
* 這個(gè)方法只會(huì)執(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);
}
}
}
}
以上這篇對(duì)handlerexecutionchain類的深入理解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程讓步y(tǒng)ield用法實(shí)例分析
這篇文章主要介紹了Java線程讓步y(tǒng)ield用法,結(jié)合實(shí)例形式分析了java中yield()方法的功能、原理及線程讓步操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09
Eclipse搭建spring開發(fā)環(huán)境圖文教程(推薦)
下面小編就為大家?guī)硪黄狤clipse搭建spring開發(fā)環(huán)境圖文教程(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Java this super代碼實(shí)例及使用方法總結(jié)
這篇文章主要介紹了Java this super代碼實(shí)例及使用方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringMVC中DispatcherServlet的HandlerMapping詳解
這篇文章主要介紹了SpringMVC中DispatcherServlet的HandlerMapping詳解,上回說的Handler,我們說是處理特定請(qǐng)求的,也就是說,不是所有的請(qǐng)求都能處理,那么問題來了,我們?cè)踔滥膫€(gè)請(qǐng)求是由哪個(gè)Handler處理的呢,需要的朋友可以參考下
2023-10-10 
