日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

HandlerInterceptor和HandlerExceptionResolver 如何在DispatcherServlet中生效?

發(fā)布時間:2024/9/30 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HandlerInterceptor和HandlerExceptionResolver 如何在DispatcherServlet中生效? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

HandlerInterceptor攔截Controller,實現(xiàn)pre和post方法。

HandlerExceptionResolver全局try-catch ?處理Controller里的異常。

這兩個類如何在DispatcherServlet中生效?

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// Determine handler for the current request.mappedHandler = getHandler(processedRequest);if (mappedHandler == null || mappedHandler.getHandler() == null) {noHandlerFound(processedRequest, response);return;}// Determine handler adapter for the current request.//判斷這個請求到底用哪個controller的哪個方法來處理。HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// Process last-modified header, if supported by the handler.String method = request.getMethod();boolean isGet = "GET".equals(method);if (isGet || "HEAD".equals(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());if (logger.isDebugEnabled()) {logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);}if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}}// 在執(zhí)行請求controller之前的操作,跟進可以看到里面是拿到所有注冊的 HandlerInterceptor, 執(zhí)行HandlerInterceptor.preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)方法。if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// Actually invoke the handler.//真正的去調(diào)用controller方法的地方,這里的ha是 AbstractHandlerMethodAdapter ,可以跟進看看其子類RequestMappingHandlerAdapter的 handleInternal()方法。真正執(zhí)行controller方法的是 mav = invokeHandlerMethod(request, response, handlerMethod);這一步mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);//在執(zhí)行請求controller之后的操作,跟進可以看到里面是拿到所有注冊的 HandlerInterceptor, 執(zhí)行HandlerInterceptor.postHandle(request, response, this.handler, mv)方法。mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException = ex;}catch (Throwable err) {// As of 4.3, we're processing Errors thrown from handler methods as well,// making them available for @ExceptionHandler methods and other scenarios.dispatchException = new NestedServletException("Handler dispatch failed", err);}//上面的兩個catch都可以看到用dispatchException 接收了異常,然后這一步是進行異常處理,可以看到的是用HandlerExceptionResolver 進行遍歷處理異常,只要處理到handlerExceptionResolver.resolveException(request, response, handler, ex)不為空的返回即break.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException("Handler processing failed", err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {// Instead of postHandle and afterCompletionif (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}

總結(jié)

以上是生活随笔為你收集整理的HandlerInterceptor和HandlerExceptionResolver 如何在DispatcherServlet中生效?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。