springMVC大纲

时间:2021-07-05 18:54:01   收藏:0   阅读:0

一、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处理器适配器

3.4 WebDataBinderFactory 数据绑定工厂

3.5 HandlerMethodArgumentResolver 参数处理器

3.6HandlerMethodReturnValueHandler 返回结果处理器

3.7 HandlerExceptionResolver异常处理器

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!