Day3 每日打卡 -简单可预期

MVC

转:https://www.pdai.tech/md/spring/spring-x-framework-springmvc-source-2.html#%E6%98%A0%E5%B0%84%E5%92%8C%E9%80%82%E9%85%8D%E5%99%A8%E5%A4%84%E7%90%86

什么是MVC

​ 用一种业务逻辑、数据、界面显示分离的方法,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

MVC英文是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计规范。本质上也是一种解耦。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MVvqWwMg-1662133965051)(${graph}/spring-springframework-mvc-4.png)]

Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。通常模型对象负责在数据库中存取数据。

View(视图)是应用程序中处理数据显示的部分。通常视图是依据模型数据创建的。

Controller(控制器)是应用程序中处理用户交互的部分。通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据

什么是spring MVC

简单而言,Spring MVC是Spring在Spring Container Core和AOP等技术基础上,遵循上述Web MVC的规范推出的web开发框架,目的是为了简化Java栈的web开发。@pdai

(PS:从我的理解就上述一句话,为了读者学习,这里找了下kaitao老师写的SpringMVC的介绍)

Spring Web MVC 是一种基于Java 的实现了Web MVC 设计模式的请求驱动类型的轻量级Web 框架,即使用了MVC 架 构模式的思想,将 web 层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开 发,Spring Web MVC 也是要简化我们日常Web 开发的。

相关特性如下

  • 让我们能非常简单的设计出干净的Web 层和薄薄的Web 层;
  • 进行更简洁的Web 层的开发;
  • 天生与Spring 框架集成(如IoC 容器、AOP 等);
  • 提供强大的约定大于配置的契约式编程支持;
  • 能简单的进行Web 层的单元测试;
  • 支持灵活的URL 到页面控制器的映射;
  • 非常容易与其他视图技术集成,如 Velocity、FreeMarker 等等,因为模型数据不放在特定的 API 里,而是放在一个 Model 里(Map 数据结构实现,因此很容易被其他框架使用);
  • 非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;
  • 提供一套强大的JSP 标签库,简化JSP 开发;
  • 支持灵活的本地化、主题等解析;
  • 更加简单的异常处理;
  • 对静态资源的支持;
  • 支持Restful 风格。

DispatcherServlet处理思想

​ DispatcherServlet是一个java Servlet,它接受前端的所有请求(过滤后,过滤是服务器(Tomcat)处理)。DispatcherServlet根据请求的url找到合适的handler去处理。

原来的一个请求对应一个servlet相比,现在只使用了一个servlet去处理所有的请求。

流程

首先让我们整体看一下Spring Web MVC 处理请求的流程:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xix0c8Is-1662133965052)(${graph}/spring-springframework-mvc-5.png)]

核心架构的具体流程步骤如下:

  1. 首先用户发送请求——>DispatcherServlet,前端控制器收到请求后自己不进行处理,而是委托给其他的解析器进行 处理,作为统一访问点,进行全局的流程控制;
  2. DispatcherServlet——>HandlerMapping, HandlerMapping 将会把请求映射为 HandlerExecutionChain 对象(包含一 个Handler 处理器(页面控制器)对象、多个HandlerInterceptor 拦截器)对象,通过这种策略模式,很容易添加新 的映射策略;
  3. DispatcherServlet——>HandlerAdapter,HandlerAdapter 将会把处理器包装为适配器,从而支持多种类型的处理器, 即适配器设计模式的应用,从而很容易支持很多类型的处理器;
  4. HandlerAdapter——>处理器功能处理方法的调用,HandlerAdapter 将会根据适配的结果调用真正的处理器的功能处 理方法,完成功能处理;并返回一个ModelAndView 对象(包含模型数据、逻辑视图名);
  5. ModelAndView 的逻辑视图名——> ViewResolver,ViewResolver 将把逻辑视图名解析为具体的View,通过这种策 略模式,很容易更换其他视图技术;
  6. View——>渲染,View 会根据传进来的Model 模型数据进行渲染,此处的Model 实际是一个Map 数据结构,因此 很容易支持其他视图技术;
  7. 返回控制权给DispatcherServlet,由DispatcherServlet 返回响应给用户,到此一个流程结束。

补充部分

  1. LocaleResolver

在视图解析/渲染时,还需要考虑国际化(Local),显然这里需要有LocaleResolver.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nl3oUMwh-1662133965053)(${graph}/spring-springframework-mvc-6.png)]

2.ThemeResolver

如何控制视图样式呢?SpringMVC中还设计了ThemeSource接口和ThemeResolver,包含一些静态资源的集合(样式及图片等),用来控制应用的视觉风格。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tnw5jv4p-1662133965054)(${graph}/spring-springframework-mvc-9.png)]

  1. 对于文件的上传请求

对于常规请求上述流程是合理的,但是如果是文件的上传请求,那么就不太一样了;所以这里便出现了MultipartResolver。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0WgGyuaE-1662133965054)(${graph}/spring-springframework-mvc-7.png)]

DispatcherServlet初始化过程

DispatcherServlet首先是Sevlet,Servlet有自己的生命周期的方法(init,destory等),那么我们在看DispatcherServlet初始化时首先需要看源码中DispatcherServlet的类结构设计。

首先我们看DispatcherServlet的类结构关系,在这个类依赖结构中找到init的方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jGTylRJ1-1662133965055)(${graph}/spring-springframework-mvc-11.png)]

DispatcherServlet请求流程

doGet入口

我们知道servlet处理get请求是doGet方法,所以我们去找DispatcherServlet类结构中的doGet方法。

@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

	processRequest(request, response);
}

processRequest处理请求的方法如下:

/**
  * Process this request, publishing an event regardless of the outcome.
  * <p>The actual event handling is performed by the abstract
  * {@link #doService} template method.
  */
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

      // 计算处理请求的时间
      long startTime = System.currentTimeMillis();
      Throwable failureCause = null;

      LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
      LocaleContext localeContext = buildLocaleContext(request);

      RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
      ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

      WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
      asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());

      // 初始化context
      initContextHolders(request, localeContext, requestAttributes);

      try {
        // 看这里
        doService(request, response);
      }
      catch (ServletException | IOException ex) {
        failureCause = ex;
        throw ex;
      }
      catch (Throwable ex) {
        failureCause = ex;
        throw new NestedServletException("Request processing failed", ex);
      }

      finally {
        // 重置context
        resetContextHolders(request, previousLocaleContext, previousAttributes);
        if (requestAttributes != null) {
          requestAttributes.requestCompleted();
        }
        logResult(request, response, failureCause, asyncManager);
        publishRequestHandledEvent(request, response, startTime, failureCause);
      }
}

本质上就是调用doService方法,由DispatchServlet类实现:

/**
  * Exposes the DispatcherServlet-specific request attributes and delegates to {@link #doDispatch}
  * for the actual dispatching.
  */
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
  logRequest(request);

      // 保存下请求之前的参数.
      Map<String, Object> attributesSnapshot = null;
      if (WebUtils.isIncludeRequest(request)) {
        attributesSnapshot = new HashMap<>();
        Enumeration<?> attrNames = request.getAttributeNames();
        while (attrNames.hasMoreElements()) {
          String attrName = (String) attrNames.nextElement();
          if (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {
            attributesSnapshot.put(attrName, request.getAttribute(attrName));
          }
        }
      }

      // 方便后续 handlers 和 view 要使用它们.
      request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
      // localeResolver解析器
      request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
      // themeResolver解析器
      request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
      request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());

      if (this.flashMapManager != null) {
        FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
        if (inputFlashMap != null) {
          request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
        }
        request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
        request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
      }

      RequestPath previousRequestPath = null;
      if (this.parseRequestPath) {
        previousRequestPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
        ServletRequestPathUtils.parseAndCache(request);
      }

      try {
        // 看这里,终于将这个请求分发出去了
        doDispatch(request, response);
      }
      finally {
        if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
          // Restore the original attribute snapshot, in case of an include.
          if (attributesSnapshot != null) {
            restoreAttributesAfterInclude(request, attributesSnapshot);
          }
        }
        if (this.parseRequestPath) {
          ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
        }
      }
}
请求分发

doDispatch方法是真正处理请求的核心方法

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);

          // 根据request获取匹配的handler.
          mappedHandler = getHandler(processedRequest);
          if (mappedHandler == null) {
            noHandlerFound(processedRequest, response);
            return;
          }

          // 根据handler获取匹配的handlerAdapter
          HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

          String method = request.getMethod();
          boolean isGet = HttpMethod.GET.matches(method);
          if (isGet || HttpMethod.HEAD.matches(method)) {
            long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
            if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
              return;
            }
          }

          // 拦截器前置处理
          if (!mappedHandler.applyPreHandle(processedRequest, response)) {
            return;
          }

          // 真正handle处理,并返回modelAndView
          mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

          if (asyncManager.isConcurrentHandlingStarted()) {
            return;
          }

          // 通过视图的prefix和postfix获取完整的视图名
          applyDefaultViewName(processedRequest, mv);

          // 拦截器后置处理
          mappedHandler.applyPostHandle(processedRequest, response, mv);
        }
        catch (Exception ex) {
          dispatchException = ex;
        }
        catch (Throwable err) {
          dispatchException = new NestedServletException("Handler dispatch failed", err);
        }

        // 处理ModelandView mv , 视图解析 和视图渲染
        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 afterCompletion
          if (mappedHandler != null) {
            mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
          }
        }
        else {
          // Clean up any resources used by a multipart request.
          if (multipartRequestParsed) {
            cleanupMultipart(processedRequest);
          }
        }
      }
}

映射和适配器处理
// 最终调用controller中对应的方法处理 
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
视图渲染

接下来继续执行processDispatchResult方法,对视图和model(如果有异常则对异常处理)进行处理(显然就是渲染页面了)

/**
  * Handle the result of handler selection and handler invocation, which is
  * either a ModelAndView or an Exception to be resolved to a ModelAndView.
  */
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
    @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
    @Nullable Exception exception) throws Exception {

      boolean errorView = false;

      // 如果处理过程有异常,则异常处理
      if (exception != null) {
        if (exception instanceof ModelAndViewDefiningException) {
          logger.debug("ModelAndViewDefiningException encountered", exception);
          mv = ((ModelAndViewDefiningException) exception).getModelAndView();
        }
        else {
          Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
          mv = processHandlerException(request, response, handler, exception);
          errorView = (mv != null);
        }
      }

      // 是否需要渲染视图
      if (mv != null && !mv.wasCleared()) {
        render(mv, request, response); // 渲染视图
        if (errorView) {
          WebUtils.clearErrorRequestAttributes(request);
        }
      }
      else {
        if (logger.isTraceEnabled()) {
          logger.trace("No view rendering, null ModelAndView returned.");
        }
      }

      if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
        // Concurrent handling started during a forward
        return;
      }

      if (mappedHandler != null) {
        // Exception (if any) is already handled..
        // 拦截器最后的处理
        mappedHandler.triggerAfterCompletion(request, response, null);
      }
}

接下来显然就是渲染视图了, spring在initStrategies方法中初始化的组件(LocaleResovler等)就派上用场了。

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
      // Determine locale for request and apply it to the response.
      Locale locale =
          (this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
      response.setLocale(locale);

      // view
      View view;
      String viewName = mv.getViewName();
      if (viewName != null) {
        // We need to resolve the view name.
        view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
        if (view == null) {
          throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
              "' in servlet with name '" + getServletName() + "'");
        }
      }
      else {
        // No need to lookup: the ModelAndView object contains the actual View object.
        view = mv.getView();
        if (view == null) {
          throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
              "View object in servlet with name '" + getServletName() + "'");
        }
      }

      // Delegate to the View object for rendering.
      if (logger.isTraceEnabled()) {
        logger.trace("Rendering view [" + view + "] ");
      }
      try {
        if (mv.getStatus() != null) {
          response.setStatus(mv.getStatus().value());
        }
        view.render(mv.getModelInternal(), request, response);
      }
      catch (Exception ex) {
        if (logger.isDebugEnabled()) {
          logger.debug("Error rendering view [" + view + "]", ex);
        }
        throw ex;
      }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值