HandlerExecutionChain類比較簡單,好理解。
1
2
3
4
|
/* * 處理器執行鏈由處理器對象和攔截器組成。 */ public class HandlerExecutionChain { |
下面是類的部分屬性。
1
2
3
4
5
|
private final Object handler; //處理器對象。 private HandlerInterceptor[] interceptors; //攔截器數組 private List<HandlerInterceptor> interceptorList; //攔截器列表 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * 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. * 執行已經注冊的攔截的 preHandle()方法。如果返回true,則執行鏈可以執行下一個攔截器的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 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
|
/* * 執行已經注冊的攔截器 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); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 這個方法只會執行preHandle()方法已經成功執行并且返回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類的深入理解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。