zoukankan      html  css  js  c++  java
  • 后端——框架——视图层框架——spring_mvc——《官网》阅读笔记——第一章节2(核心流程)

      Spring MVC处理请求的核心流程如下:

      第一步The WebApplicationContext is searched for and bound in the request as an attribute that the controller and other elements in the process can use. It is bound by default under the DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE key

        通过request对象的DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE获取WebApplicationContext对象。查看DispatcherServlet的doService的部分源码如下:

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

      

      第二步The locale resolver is bound to the request to let elements in the process resolve the locale to use when processing the request (rendering the view, preparing data, and so on). If you do not need locale resolving, you do not need the locale resolver

    通过LocaleResolver获取国际化信息,通常情况下不配置,使用它的默认值。可以通过查看DispatcherServlet.properties文件,看到默认的LocaleResolver类型是AcceptHeaderLocaleResolver。查看它的resolveLocale源码如下:

    首先查找默认的国际化,或通过请求头部的Accept-language属性获取国际化信息。

    其次调用request.getLocale,获取请求的国际化信息。

    最后查找request支持的国际化列表中选择。

    通常情况下国际化信息都是默认的国际化,或Accept-language的值。

     

    第三步The theme resolver is bound to the request to let elements such as views determine which theme to use. If you do not use themes, you can ignore it

    通过ThemeResolver配置网站的样式,本处省略。

     

      第四步If you specify a multipart file resolver, the request is inspected for multiparts. If multiparts are found, the request is wrapped in a MultipartHttpServletRequest for further processing by other elements in the process. See Multipart Resolver for further information about multipart handling

    当为上传文件的请求时,若指定了MultipartResolver,request对象会被封装为MultipartHttpServletRequest,被后续处理。查看DispatcherServlet.properties文件会发现它是没有默认值的,需要手动配置。

     

      第五步An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) is executed in order to prepare a model or rendering. Alternatively, for annotated controllers, the response can be rendered (within the HandlerAdapter) instead of returning a view

    这一段描述的就是DispatcherServlet的doService--->doDispatch方法的逻辑。

    processedRequest = checkMultipart(request);
    multipartRequestParsed = (processedRequest != request);
    
    // Determine handler for the current request.
    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;
    	}
    }
    
    if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    	return;
    }
    
    // Actually invoke the handler.
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    
    if (asyncManager.isConcurrentHandlingStarted()) {
    	return;
    }
    
    applyDefaultViewName(processedRequest, mv);
    mappedHandler.applyPostHandle(processedRequest, response, mv);
    

      

      第六步If a model is returned, the view is rendered. If no model is returned (maybe due to a preprocessor or postprocessor intercepting the request, perhaps for security reasons),no view is rendered, because the request could already have been fulfilled

    当ModelAndView对象返回后,生成视图。若没有ModelAndView返回,不生成视图。

      ModelAndView将视图层划分为两个部分,第一部分为Model(数据),第二部分为数据的展示形式(视图)。其中Model的类型为ModelMap,它继承LinkedHashMap,格式为key-value的形式。视图的种类也很多,XML,JSP,数据模板等等,每一种视图对应一种ViewResolver。

       第七步The HandlerExceptionResolver beans declared in the WebApplicationContext are used to resolve exceptions thrown during request processing. Those exception resolvers allow customizing the logic to address exceptions

    当请求过程中发生异常时,HandlerExceptionResolver会处理。处理逻辑跟异常的类型和HandlerExceptionResolver的类型有关。默认的HandlerExceptionResolver有

    org.springframework.web.servlet.HandlerExceptionResolver=
    org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,
    org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,
    org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
  • 相关阅读:
    用 xampp 在 windows/Linux 下搭建代理服务器
    DOM 元素 属性和方法
    JavaScript入门培训材料(Copy至此以作备份)
    JavaScript 关键字快速匹配
    JS参考书籍
    chrome 阻止跨域操作的解决方法 --disable-web-security
    可输入自动匹配Select——jquery ui autocomplete
    【消息队列】如何保证消息的顺序性
    【消息队列】如何处理消息丢失的问题
    【消息队列】kafka是如何保证消息不被重复消费的
  • 原文地址:https://www.cnblogs.com/rain144576/p/12903093.html
Copyright © 2011-2022 走看看