欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC请求处理大致流程源码学习笔记

程序员文章站 2022-06-02 19:06:31
...

请求处理大致流程:doDispatch()详细细节

@SuppressWarnings("serial")
public class DispatcherServlet extends FrameworkServlet {

	@Override
	protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			doDispatch(request, response);
		}
	}
	
	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);
				//如果是文件上传请求,就不能用原来的request,如果是就用原来的
				multipartRequestParsed = (processedRequest != request);

				// Determine handler for the current request.
				//2.根据当前请求地址,找到用哪个(处理器)Handler
				mappedHandler = getHandler(processedRequest);
				
				//3.如果没有找到哪个处理器(控制器)能处理这个请求就404,或者抛异常
				if (mappedHandler == null || mappedHandler.getHandler() == null) {
					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);
				if (isGet || "HEAD".equals(method)) {
					long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
					if (logger.isDebugEnabled()) {
						logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
					}
					if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
						return;
					}
				}

				if (!mappedHandler.applyPreHandle(processedRequest, response)) {
					return;
				}
				
				// Actually invoke the handler.
				//5.用适配器真正执行处理器的方法,将目标方法执行完成后的返回值作为视图名,设置保存到ModelAndView中
				//无论目标方法怎写,最终适配器执行完成以后都会将执行后的信息封装成ModelAndView
				//处理(控制器)器的方法被调用
				//控制器(Controller),处理器(Handler)
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
				
				//异步处理器判断,是否是异步方法
				if (asyncManager.isConcurrentHandlingStarted()) {
					return;
				}
				
				//如果没有视图名,使用默认的视图名字
				applyDefaultViewName(request, mv);
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			catch (Exception ex) {
				dispatchException = ex;
			}
			//转发到目标页面,页面渲染
			//6.根据方法最终执行后封装的ModelAndView,转发到对应页面,而且ModelAndView中的数据可以从请求域中获取
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}
		catch (Exception ex) {
		triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
		}
		catch (Error err) {
			triggerAfterCompletionWithError(processedRequest, response, mappedHandler, 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);
				}
			}
		}
	}
}

noHandlerFound

	源码:
	protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (pageNotFoundLogger.isWarnEnabled()) {
			pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) +
					"] in DispatcherServlet with name '" + getServletName() + "'");
		}
		if (this.throwExceptionIfNoHandlerFound) {
			ServletServerHttpRequest sshr = new ServletServerHttpRequest(request);
			throw new NoHandlerFoundException(
					sshr.getMethod().name(), sshr.getServletRequest().getRequestURI(), sshr.getHeaders());
		}
		else {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}
相关标签: spring mvc