SpringMVC执行的流程

执行流程

  1. 所有的请求,前端控制器DispatcherServlet收到请求,调用doDispatch()方法进行处理。
  2. 调用getHandler()方法,根据HandlerMapping中保存的请求映射信息找到,可以处理当前请求的处理器执行链,执行链包括目标方法、拦截器。
  3. 根据当前处理器找到该类的HandlerAdapter适配器。
  4. 拦截器的preHandler()方法先执行。
  5. 然后适配器再执行目标方法。有@ModelAttribute标注的方法会提前于目标方法执行。执行目标方法的时候要确定目标方法用的参数。如果确定参数,看另一篇DispatcherServlet前端控制器结构分析
  6. 拦截器的postHandler()方法执行。
  7. 处理请求的结果(即渲染页面)。如果有异常,使用异常解析器处理异常,处理完后还会返回ModelAndView。视图解析器根据视图名得到视图对象,然后调用render()方法进行渲染。渲染流程见SpringMVC视图源码解析。页面渲染完成之后还会调用拦截器的afterCompletion()方法。

源代码

DispatcherServlet类中的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);

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

doDispatch()方法中的渲染页面的方法processDispatchResult()

	/**
	 * 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);
			}
		}

		// 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..
			mappedHandler.triggerAfterCompletion(request, response, null);
		}
	}

请求执行的流程图

SpringMVC执行的流程_第1张图片

你可能感兴趣的:(SpringMVC,SpringMVC,执行流程)