springMVC大纲

2021-07-28 01:56

阅读:789

标签:multipart   bind   util   lse   tpm   art   factory   注解   cte   一、springmvc是什么? 1、是一个表现层的框架。 2、它是在这个网络请求的过程中担任的是一个处理 HttpServletRequest, HttpServletResponse的方法。 它的上游是tomcat是一个servlet容器,功能是将网络请求封装成HttpServletRequest, HttpServletResponse。 它的下游是提供数据的方法。springMVC就是这样中间衔接和封装的。 二、原理分析 Dispatcher的核心流程代码如下 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); //通过HandlerMapping找到对应的 HandlerExecutionChain //HandlerExecutionChain 包含 handler和 HandlerInterceptor mappedHandler = getHandler(processedRequest); if (mappedHandler == null) { noHandlerFound(processedRequest, response); return; } // 包装handler变成HandlerAdapter //因为可能有不同情况转化成处理器 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 (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } //HandlerInterceptor的preHandle方法。 //如果有一个执行失败,就执行完对应的afterCompletion的方法 if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } // 实际调用handler方法 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } applyDefaultViewName(processedRequest, mv); //执行HandlerInterceptor的postHandle方法 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); } // 处理结果 用视图解析器获得视图,以及视图的渲染 processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } catch (Exception ex) { //异常情况调用HandlerInterceptor的afterCompletion方法 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 afterCompletion if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } } else { // Clean up any resources used by a multipart request. if (multipartRequestParsed) { cleanupMultipart(processedRequest); } } } } 三、springMVC的扩展点 从流程开始往后一个一个来。 3.1 HandlerMapping资源文件映射器。 这里面最重要的就是RequestMappingHandlerMapping啦 3.2HandlerInterceptor拦截器 3.3HandlerAdapter处理器适配器 SimpleServletHandlerAdapter处理Servlet接口的 SimpleControllerHandlerAdapter处理实现Controller接口的 HttpRequestHandlerAdapter处理HttpRequestHandler接口的 ? 不要向Controller那样最后还要生成视图,比如ResourceHttpRequestHandler。 RequestMappingHandlerAdapter处理注解的@Controller 3.4 WebDataBinderFactory 数据绑定工厂 WebBindingInitializer 可以通过 @initBinder来实现对应的controller的,可以是实现WebBindingInitializer处理 全局的。 WebDataBinder ConversionService Converter 3.5 HandlerMethodArgumentResolver 参数处理器 3.6HandlerMethodReturnValueHandler 返回结果处理器 HttpMessageConverter 3.7 HandlerExceptionResolver异常处理器springMVC大纲标签:multipart   bind   util   lse   tpm   art   factory   注解   cte   原文地址:https://www.cnblogs.com/matteo-catari/p/14967526.html


评论


亲,登录后才可以留言!