请求处理的大致流程和doDispatch方法

1、请求处理流程

请求处理流程

2、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 {
    
    
				//1、检查是否文件上传请求
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				// Determine handler for the current request.
				//2、根据请求,找到处理的Controller类
				mappedHandler = getHandler(processedRequest);
				if (mappedHandler == null) {
    
    
					//3、如果没找到处理的控制器,就404,或者抛异常
					noHandlerFound(processedRequest, response);
					return;
				}

				// Determine handler adapter for the current request.
				//4、拿到可以执行这个类的所有方法的适配器(反射工具)
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

				// Process last-modified header, if supported by the handler.
				String method = request.getMethod();
				boolean isGet = "GET".equals(method);
				//Get请求
				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.
				//5、控制器的方法被调用:适配器执行目标方法,
				//将目标方法完成后的返回值作为视图名,设置保存到ModelAndView中。
				//目标方法无论怎么写,最终适配器执行完成后都会将执行后的信息封装为ModelAndView
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
				//异步处理器判断,如果是异步,请求就结束了
				if (asyncManager.isConcurrentHandlingStarted()) {
    
    
					return;
				}
				//如果没有视图名,,设置一个默认的视图名
				applyDefaultViewName(processedRequest, mv);
				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);
			}
			//6、转发到目标页面
			//根据方法最终执行完成后封装的ModelAndView,转发到对应页面,
			//并且ModelAndView数据可以从请求域中获取
			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. 所有请求过来,DispatcherServlet收到请求
  2. 调用doDispatch方法进行处理
    (1)getHandler():根据当期请求地址,找到能够处理这个请求的目标处理器类;
    根据当前请求,在HandlerMapping中,找这个请求的映射信息,获取到目标处理器类
    (2) getHandlerAdapter():根据当前处理器获取到能执行这个处理器方法的适配器;
    根据当前处理器类,找到当前类的HandlerAdapter(适配器)
    (3)使用刚才获取到的适配器执行目标方法;
    (4)目标方法执行完成后,会返回一个ModelAndView对象;
    (5)根据ModelAndView的信息转发到具体的页面,并可以在请求域中取出ModelAndView中的数据

3、SpringMVC 9大组件

springmvc9大组件

猜你喜欢

转载自blog.csdn.net/weixin_44134725/article/details/112447998
今日推荐