Spring MVC——4. 处理请求

每个请求到达时,都会调用 Servlet 的 service 方法。
service
processRequest
doService核心方法: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 {
			// 如果是 MultipartContent 类型的 request,则转换 request 为 MultipartHttpServletRequest 类型的 request
			processedRequest = checkMultipart(request);
			multipartRequestParsed = (processedRequest != request);

			// Determine handler for the current request.
			// 1. 根据 request 信息寻找对应的 Handler
			mappedHandler = getHandler(processedRequest);
			if (mappedHandler == null) {
				// 如果没有找到对应的 handler,则通过 response 反馈错误信息
				noHandlerFound(processedRequest, response);
				return;
			}

			// Determine handler adapter for the current request.
			// 2. 根据当前的 handler 寻找对应的 HandlerAdapter
			HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

			// Process last-modified header, if supported by the handler.
			// 如果当前 handler 支持 last-modified 头处理
			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;
				}
			}
			// 3. 调用拦截器:拦截器的 preHandle 方法的调用
			if (!mappedHandler.applyPreHandle(processedRequest, response)) {
				return;
			}

			// Actually invoke the handler.
			// 4. 方法调用
			mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

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

			// 视图名称转换应用于需要添加前缀后缀的情况
			applyDefaultViewName(processedRequest, mv);
			// 5. 调用拦截器:拦截器的 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);
		}
		// 6. 处理方法返回(返回页面、属性)
		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) {
				// 7. 调用拦截器:请求完成后处理
				mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
			}
		}
		else {
			// Clean up any resources used by a multipart request.
			if (multipartRequestParsed) {
				cleanupMultipart(processedRequest);
			}
		}
	}
}
  1. 根据 request 信息寻找对应的 Handler
    getHandler
    getHandler
    getHandlerInternal
  2. 根据当前的 handler 寻找对应的 HandlerAdapter
    getHandlerAdapter
  3. 调用拦截器:拦截器的 preHandle 方法的调用
    applyPreHandle
  4. 方法调用
    handle
    handleInternal
    invokeHandlerMethod
    invokeAndHandle
    invokeForRequest
    getMethodArgumentValues
    doInvoke
    createDataBinderFactory
  5. 调用拦截器:拦截器的 postHandle 方法的调用
    applyPostHandle
  6. 处理方法返回(返回页面、属性)
    processDispatchResult
    render
    render
    renderMergedOutputModel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值