對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類的深入理解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java 定時器Timer和TimerTask的使用詳解(執(zhí)行和暫停)
這篇文章主要介紹了java 定時器Timer和TimerTask的使用詳解(執(zhí)行和暫停),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Java調用接口如何獲取json數(shù)據(jù)解析后保存到數(shù)據(jù)庫
這篇文章主要介紹了Java調用接口如何獲取json數(shù)據(jù)解析后保存到數(shù)據(jù)庫問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

