• 【零基础入门SpringMVC】第六期——尾声


    一、注解配置SpringMVC

    • 采用全注解开发,替代我们的web.xmlSpringMVC的核心配置文件

    • 我们需要创建对应的配置类,继承AbstractAnnotationConfigDispatcherServletInitializer

      • 使用的Servlet版本要求在3.0以上
      • 项目启动后容器会找到配置了,基于我们的配置来初始化Servlet的上下文
    • 接下来给出模板代码及注释,演示如何使用注解进行配置
      在这里插入图片描述

    准备一个新的模块,无需创建 web.xml,打包方式仍为war,导入之前的依赖,手动创建webappWEB-INF目录

    • 编写我们的核心配置类 WebInit

      package com.atguigu.mvc.config;
      
      import org.springframework.web.filter.CharacterEncodingFilter;
      import org.springframework.web.filter.HiddenHttpMethodFilter;
      import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
      
      import javax.servlet.Filter;
      
      /**
       * 代替我们的web.xml
       * @author Bonbons
       * @version 1.0
       */
      public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
          /**
           * 指定Spring配置类
           * @return
           */
          @Override
          protected Class<?>[] getRootConfigClasses() {
              return new Class[]{SpringConfig.class};
          }
      
          /**
           * 指定SpringMVC配置类
           * @return
           */
          @Override
          protected Class<?>[] getServletConfigClasses() {
              return new Class[]{WebConfig.class};
          }
      
          /**
           * 指定前端控制器的映射规则, url-pattern
           * @return
           */
          @Override
          protected String[] getServletMappings() {
              return new String[]{"/"};
          }
      
          /**
           * 配置我们的过滤器
           * @return
           */
          @Override
          protected Filter[] getServletFilters() {
              // 编码过滤器
              CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
              characterEncodingFilter.setEncoding("UTF-8");
              characterEncodingFilter.setForceResponseEncoding(true);
              // 隐藏过滤器,将post转换为delete和put [没有啥初始化参数,就是注册一下就能用]
              HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
              // 返回我们创建的过滤器
              return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
    • 编写我们 SpringMVC 的核心配置类 WebConfig

      package com.atguigu.mvc.config;
      
      import com.atguigu.mvc.interceptor.TestInterceptor;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.context.ContextLoader;
      import org.springframework.web.context.WebApplicationContext;
      import org.springframework.web.multipart.MultipartFile;
      import org.springframework.web.multipart.MultipartResolver;
      import org.springframework.web.multipart.commons.CommonsMultipartResolver;
      import org.springframework.web.servlet.HandlerExceptionResolver;
      import org.springframework.web.servlet.ViewResolver;
      import org.springframework.web.servlet.config.annotation.*;
      import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
      import org.thymeleaf.spring5.SpringTemplateEngine;
      import org.thymeleaf.spring5.view.ThymeleafViewResolver;
      import org.thymeleaf.templatemode.TemplateMode;
      import org.thymeleaf.templateresolver.ITemplateResolver;
      import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
      
      import java.util.List;
      import java.util.Properties;
      
      /**
       * @author Bonbons
       * @version 1.0
       */
      @Configuration // 声明配置类
      @ComponentScan ("com.atguigu.mvc.controller")// 扫描组件
      @EnableWebMvc // 开启注解驱动
      public class WebConfig implements WebMvcConfigurer {
          /**
           * 配置默认的Servlet,也就是处理我们的静态资源
           * @param configurer
           */
          @Override
          public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
              WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
          }
      
          /**
           * 配置拦截器
           * @param registry
           */
          @Override
          public void addInterceptors(InterceptorRegistry registry) {
              // 创建我们自定义拦截器的对象
              TestInterceptor testInterceptor = new TestInterceptor();
              // 设置拦截规则
              registry.addInterceptor(testInterceptor).addPathPatterns("/**");
              WebMvcConfigurer.super.addInterceptors(registry);
          }
      
          /**
           * 配置视图控制器
           * @param registry
           */
          @Override
          public void addViewControllers(ViewControllerRegistry registry) {
              // 前面的viewController是请求路径,后面的viewName是视图名
              registry.addViewController("/hello").setViewName("hello");
              WebMvcConfigurer.super.addViewControllers(registry);
          }
      
          /**
           * 配置我们的文件上传解析器
           * @return
           */
          public MultipartResolver multipartResolver(){
              // 上面这个类型实际是一个接口,所以我们需要创建对应实现类的对象
              CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
              // 返回
              return commonsMultipartResolver;
          }
      
          @Override
          public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
      
              SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
              // Properties 集合存储异常和跳转页面的映射
              Properties prop = new Properties();
              // 添加
              prop.setProperty("java.lang.ArithmeticException", "error");
              // 将其添加到我们的SimpleMappingExceptionResolver自定义异常对象中
              exceptionResolver.setExceptionMappings(prop);
              // 将异常信息添加到共享域中
              exceptionResolver.setExceptionAttribute("exception");
              // 将配置添加到我们的解析器中
              resolvers.add(exceptionResolver);
              WebMvcConfigurer.super.configureHandlerExceptionResolvers(resolvers);
          }
      
          //配置生成模板解析器
          @Bean
          public ITemplateResolver templateResolver() {
              WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
              // ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过WebApplicationContext 的方法获得
              ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(webApplicationContext.getServletContext());
              templateResolver.setPrefix("/WEB-INF/templates/");
              templateResolver.setSuffix(".html");
              templateResolver.setCharacterEncoding("UTF-8");
              templateResolver.setTemplateMode(TemplateMode.HTML);
              return templateResolver;
          }
      
          //生成模板引擎并为模板引擎注入模板解析器
          @Bean
          public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
              SpringTemplateEngine templateEngine = new SpringTemplateEngine();
              templateEngine.setTemplateResolver(templateResolver);
              return templateEngine;
          }
      
          //生成视图解析器并未解析器注入模板引擎
          @Bean
          public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
              ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
              viewResolver.setCharacterEncoding("UTF-8");
              viewResolver.setTemplateEngine(templateEngine);
              return viewResolver;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
    • 编写我们Spring的核心配置类 SpringConfig 【只是定义了这个类,但是没配置】

      package com.atguigu.mvc.config;
      
      import org.springframework.context.annotation.Configuration;
      
      /**
       * @author Bonbons
       * @version 1.0
       */
      @Configuration
      public class SpringConfig {
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 编写我们拦截器的配置类 TestInterceptor

      package com.atguigu.mvc.interceptor;
      
      import org.springframework.web.servlet.HandlerInterceptor;
      import org.springframework.web.servlet.ModelAndView;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      /**
       * @author Bonbons
       * @version 1.0
       */
      public class TestInterceptor implements HandlerInterceptor {
          @Override
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
              System.out.println("TestInterceptor-->preHandle");
              return HandlerInterceptor.super.preHandle(request, response, handler);
          }
      
          @Override
          public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
              HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
          }
      
          @Override
          public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
              HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
    • 编写我们的控制器 TestController

      package com.atguigu.mvc.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      /**
       * @author Bonbons
       * @version 1.0
       */
      @Controller
      public class TestController {
          @RequestMapping("/")
          public String index(){
              return "index";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    • 编写我们的首页 index

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
      hello
      </body>
      </html>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 编写我们的 hello 页面,用于视图控制器 【还应该有个异常跳转页面 error

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
      hello
      </body>
      </html>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 这部分内容主要看一下应该如何配置,我觉得实际开发的时候,不会采用全部用注解进行配置

    在这里插入图片描述

    二、SpringMVC的执行流程

    $ SpringMVC常用组件

    • DispatcherServlet前端控制器,不需要工程师开发,由框架提供

      • 作用:统一处理请求和响应,整个流程控制的中心,由它调用其它组件处理用户的请求
    • HandlerMapping处理器映射器,不需要工程师开发,由框架提供

      • 作用:根据请求的urlmethod等信息查找Handler,即控制器方法
    • Handler处理器,需要工程师开发

      • 作用:在DispatcherServlet的控制下Handler对具体的用户请求进行处理
    • HandlerAdapter处理器适配器,不需要工程师开发,由框架提供

      • 作用:通过HandlerAdapter对处理器(控制器方法)进行执行
    • ViewResolver视图解析器,不需要工程师开发,由框架提供

      • 作用:进行视图解析,得到相应的视图,ThymeleafView、InternalResourceView、RedirectViewThymeleaf、转发、重定向】
    • View视图

      • 作用:将模型数据通过页面展示给用户

    $ DispatcherServlet初始化过程

    DispatcherServlet 本质上是一个 Servlet,所以天然的遵循 Servlet 的生命周期。所以宏观上是 Servlet 生命周期来进行调度。

    在这里插入图片描述

    a>初始化WebApplicationContext

    所在类:org.springframework.web.servlet.FrameworkServlet

    protected WebApplicationContext initWebApplicationContext() {
        WebApplicationContext rootContext =
            WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        WebApplicationContext wac = null;
    
        if (this.webApplicationContext != null) {
            // A context instance was injected at construction time -> use it
            wac = this.webApplicationContext;
            if (wac instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
                if (!cwac.isActive()) {
                    // The context has not yet been refreshed -> provide services such as
                    // setting the parent context, setting the application context id, etc
                    if (cwac.getParent() == null) {
                        // The context instance was injected without an explicit parent -> set
                        // the root application context (if any; may be null) as the parent
                        cwac.setParent(rootContext);
                    }
                    configureAndRefreshWebApplicationContext(cwac);
                }
            }
        }
        if (wac == null) {
            // No context instance was injected at construction time -> see if one
            // has been registered in the servlet context. If one exists, it is assumed
            // that the parent context (if any) has already been set and that the
            // user has performed any initialization such as setting the context id
            wac = findWebApplicationContext();
        }
        if (wac == null) {
            // No context instance is defined for this servlet -> create a local one
            // 创建WebApplicationContext
            wac = createWebApplicationContext(rootContext);
        }
    
        if (!this.refreshEventReceived) {
            // Either the context is not a ConfigurableApplicationContext with refresh
            // support or the context injected at construction time had already been
            // refreshed -> trigger initial onRefresh manually here.
            synchronized (this.onRefreshMonitor) {
                // 刷新WebApplicationContext
                onRefresh(wac);
            }
        }
    
        if (this.publishContext) {
            // Publish the context as a servlet context attribute.
            // 将IOC容器在应用域共享
            String attrName = getServletContextAttributeName();
            getServletContext().setAttribute(attrName, wac);
        }
    
        return wac;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    b>创建WebApplicationContext

    所在类:org.springframework.web.servlet.FrameworkServlet

    protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
        Class<?> contextClass = getContextClass();
        if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
            throw new ApplicationContextException(
                "Fatal initialization error in servlet with name '" + getServletName() +
                "': custom WebApplicationContext class [" + contextClass.getName() +
                "] is not of type ConfigurableWebApplicationContext");
        }
        // 通过反射创建 IOC 容器对象
        ConfigurableWebApplicationContext wac =
            (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
    
        wac.setEnvironment(getEnvironment());
        // 设置父容器
        wac.setParent(parent);
        String configLocation = getContextConfigLocation();
        if (configLocation != null) {
            wac.setConfigLocation(configLocation);
        }
        configureAndRefreshWebApplicationContext(wac);
    
        return wac;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    c>DispatcherServlet初始化策略

    FrameworkServlet创建WebApplicationContext后,刷新容器,调用onRefresh(wac),此方法在DispatcherServlet中进行了重写,调用了initStrategies(context)方法,初始化策略,即初始化DispatcherServlet的各个组件

    所在类:org.springframework.web.servlet.DispatcherServlet

    protected void initStrategies(ApplicationContext context) {
       initMultipartResolver(context);
       initLocaleResolver(context);
       initThemeResolver(context);
       initHandlerMappings(context);
       initHandlerAdapters(context);
       initHandlerExceptionResolvers(context);
       initRequestToViewNameTranslator(context);
       initViewResolvers(context);
       initFlashMapManager(context);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    $ DispatcherServlet调用组件处理请求

    a>processRequest()

    FrameworkServlet重写HttpServlet中的service()和doXxx(),这些方法中调用了processRequest(request, response)

    所在类:org.springframework.web.servlet.FrameworkServlet

    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());
    
        initContextHolders(request, localeContext, requestAttributes);
    
        try {
    		// 执行服务,doService()是一个抽象方法,在DispatcherServlet中进行了重写
            doService(request, response);
        }
        catch (ServletException | IOException ex) {
            failureCause = ex;
            throw ex;
        }
        catch (Throwable ex) {
            failureCause = ex;
            throw new NestedServletException("Request processing failed", ex);
        }
    
        finally {
            resetContextHolders(request, previousLocaleContext, previousAttributes);
            if (requestAttributes != null) {
                requestAttributes.requestCompleted();
            }
            logResult(request, response, failureCause, asyncManager);
            publishRequestHandledEvent(request, response, startTime, failureCause);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    b>doService()

    所在类:org.springframework.web.servlet.DispatcherServlet

    @Override
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logRequest(request);
    
        // Keep a snapshot of the request attributes in case of an include,
        // to be able to restore the original attributes after the include.
        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));
                }
            }
        }
    
        // Make framework objects available to handlers and view objects.
        request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
        request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
        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 requestPath = null;
        if (this.parseRequestPath && !ServletRequestPathUtils.hasParsedRequestPath(request)) {
            requestPath = 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 (requestPath != null) {
                ServletRequestPathUtils.clearParsedRequestPath(request);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    c>doDispatch()

    所在类:org.springframework.web.servlet.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:调用链
                    包含handler、interceptorList、interceptorIndex
                	handler:浏览器发送的请求所匹配的控制器方法
                	interceptorList:处理控制器方法的所有拦截器集合
                	interceptorIndex:拦截器索引,控制拦截器afterCompletion()的执行
                */
                mappedHandler = getHandler(processedRequest);
                if (mappedHandler == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }
    
                // Determine handler adapter for the current request.
               	// 通过控制器方法创建相应的处理器适配器,调用所对应的控制器方法
                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;
                    }
                }
    			
                // 调用拦截器的preHandle()
                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }
    
                // Actually invoke the handler.
                // 由处理器适配器调用具体的控制器方法,最终获得ModelAndView对象
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }
    
                applyDefaultViewName(processedRequest, mv);
                // 调用拦截器的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) {
            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);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    d>processDispatchResult()

    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);
            }
        }
    
        // Did the handler return a view to render?
        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..
            // 调用拦截器的afterCompletion()
            mappedHandler.triggerAfterCompletion(request, response, null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    $ SpringMVC的执行流程

    1. 用户向服务器发送请求,请求被SpringMVC 前端控制器 DispatcherServlet捕获。

    2. DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI),判断请求URI对应的映射:

    a) 不存在

    i. 再判断是否配置了mvc:default-servlet-handler

    ii. 如果没配置,则控制台报映射查找不到,客户端展示404错误

    在这里插入图片描述
    在这里插入图片描述

    iii. 如果有配置,则访问目标资源(一般为静态资源,如:JS,CSS,HTML),找不到客户端也会展示404错误
    在这里插入图片描述

    b) 存在则执行下面的流程

    1. 根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain执行链对象的形式返回。

    2. DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter

    3. 如果成功获得HandlerAdapter,此时将开始执行拦截器的preHandler(…)方法【正向】

    4. 提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)方法,处理请求。在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:

    a) HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定的响应信息

    b) 数据转换:对请求消息进行数据转换。如String转换成IntegerDouble

    c) 数据格式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等

    d) 数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResultError

    1. Handler执行完成后,向DispatcherServlet 返回一个ModelAndView对象。

    2. 此时将开始执行拦截器的postHandle(…)方法【逆向】。

    3. 根据返回的ModelAndView(此时会判断是否存在异常:如果存在异常,则执行HandlerExceptionResolver进行异常处理)选择一个适合的ViewResolver进行视图解析,根据ModelView,来渲染视图。

    4. 渲染视图完毕执行拦截器的afterCompletion(…)方法【逆向】。

    5. 将渲染结果返回给客户端。

    在这里插入图片描述

  • 相关阅读:
    Nacos整合Gateway实现动态路由
    深入探讨负载均衡的原理及算法
    删了很多还提示内存不足?Mac内存空间你要这样清理
    使用UDP协议实现简单的分布式日志服务, java和python
    开源一个反sql注入的asp.net core中间件
    java面向对象02——常见的设计模式
    Js实现继承的6种方式
    java-php-net-python-二手书商城系统计算机毕业设计程序
    IntelliJ IDEA Maven 项目的依赖分析
    Erlang 入门——从普通tcp到OTP框架通信
  • 原文地址:https://blog.csdn.net/qq_61323055/article/details/128064695