博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMvc 大概流程分析
阅读量:5327 次
发布时间:2019-06-14

本文共 5050 字,大约阅读时间需要 16 分钟。

DispatcherServlet的核心方法     doDispatch

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {		HttpServletRequest processedRequest = request;		HandlerExecutionChain mappedHandler = null;		int interceptorIndex = -1;		try {			ModelAndView mv;			boolean errorView = false;			try {				processedRequest = checkMultipart(request);				// Determine handler for the current request.				mappedHandler = getHandler(processedRequest, false);				if (mappedHandler == null || mappedHandler.getHandler() == 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 (logger.isDebugEnabled()) {						String requestUri = urlPathHelper.getRequestUri(request);						logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);					}					if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {						return;					}				}				// Apply preHandle methods of registered interceptors.				HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();				if (interceptors != null) {					for (int i = 0; i < interceptors.length; i++) {						HandlerInterceptor interceptor = interceptors[i];						if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {							triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);							return;						}						interceptorIndex = i;					}				}				// Actually invoke the handler.				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());				// Do we need view name translation?				if (mv != null && !mv.hasView()) {					mv.setViewName(getDefaultViewName(request));				}				// Apply postHandle methods of registered interceptors.				if (interceptors != null) {					for (int i = interceptors.length - 1; i >= 0; i--) {						HandlerInterceptor interceptor = interceptors[i];						interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);					}				}			}			catch (ModelAndViewDefiningException ex) {				logger.debug("ModelAndViewDefiningException encountered", ex);				mv = ex.getModelAndView();			}			catch (Exception ex) {				Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);				mv = processHandlerException(processedRequest, response, handler, ex);				errorView = (mv != null);			}			// Did the handler return a view to render?			if (mv != null && !mv.wasCleared()) {				render(mv, processedRequest, response);				if (errorView) {					WebUtils.clearErrorRequestAttributes(request);				}			}			else {				if (logger.isDebugEnabled()) {					logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +							"': assuming HandlerAdapter completed request handling");				}			}			// Trigger after-completion for successful outcome.			triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);		}		catch (Exception ex) {			// Trigger after-completion for thrown exception.			triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);			throw ex;		}		catch (Error err) {			ServletException ex = new NestedServletException("Handler processing failed", err);			// Trigger after-completion for thrown exception.			triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);			throw ex;		}		finally {			// Clean up any resources used by a multipart request.			if (processedRequest != request) {				cleanupMultipart(processedRequest);			}		}	}

从上面方法开始分析:

     SpringMvc通过一个 DispatcherServlet  Servlet对指定的URL进行处理, 当处理某个请求url时 DispatcherServlet  会迭代所有的 HandlerMapping  

即上述代码中的 

mappedHandler = getHandler(processedRequest, false);
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {		for (HandlerMapping hm : this.handlerMappings) {			if (logger.isTraceEnabled()) {				logger.trace(						"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");			}			HandlerExecutionChain handler = hm.getHandler(request);			if (handler != null) {				return handler;			}		}		return null;	}

  从上面代码可以获知:迭代所有的 HandlerMapping当找到合适的立刻返回,返回的对象为   HandlerExecutionChain ,

HandlerExecutionChain 对象中包含了映射到的Controller和一些 Intetceptor(拦截器).然后通过
HandlerExecutionChain 中的 getHandler()即Controller获取对应到 HandlerAdapter()
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
HandlerAdapter中包含了很多的处理 ,如参数解析() 数据绑定信息 在调用
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());方法中间包含了 迭代
HandlerExecutionChain 中的所有拦截器 ,
HandlerAdapter的
handle将调用目标Controler方法并返回对应到视图
 
 

 

转载于:https://www.cnblogs.com/coollijie/p/4244215.html

你可能感兴趣的文章
wnmp安装配置的坑
查看>>
神奇的Scala Macro之旅(二)- 一个实例
查看>>
sicily 1128. DICE
查看>>
e.Row.Attributes.Add
查看>>
SCOPE_IDENTITY()和 SELECT @@IDENTITY 的用法
查看>>
PLoP(Pattern Languages of Programs,程序设计的模式语言)
查看>>
对PostgreSQL的 SPI_prepare 的理解。
查看>>
android"百码"2——基础小知识积累(逐步完善)2015-06-15
查看>>
解决响应式布局下兼容性的问题
查看>>
京东静态网页练习记录
查看>>
Filebeat Config 参数详解:
查看>>
使用DBCP连接池对连接进行管理
查看>>
【洛谷】【堆+模拟】P2278 操作系统
查看>>
hdu3307 欧拉函数
查看>>
Spring Bean InitializingBean和DisposableBean实例
查看>>
Solr4.8.0源码分析(5)之查询流程分析总述
查看>>
[Windows Server]安装系统显示“缺少计算机所需的介质驱动程序”解决方案
查看>>
[容斥][dp][快速幂] Jzoj P5862 孤独
查看>>
Lucene 学习之二:数值类型的索引和范围查询分析
查看>>
软件开发工作模型
查看>>