时间:2021-05-19
在分析spring mvc源码之前,先看一张图:
请求处理的过程:
1.DispatcherServelt作为前端控制器,拦截request对象。
2.DispatcherServlet接收到request对象之后,查询HandlerMapping,得到一个HandlerExecutionChain对象。
3.DispatcherServlet将Handler对象交由HandlerAdapter(适配器模式的典型应用),调用相应的controller方法。
4.Controller方法返回ModelAndView对象,DispatcherServlet将view交由ViewResolver进行解析,得到相应的视图。用model渲染视图。
5.返回响应结果。
整个过程的流程其实就是DispatcherServelt中doDispatch()方法的调用过程。
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {//第一步拦截request对象,doDispatch()方法在doService()方法中被调用,request对象是经过处理的。HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {//和文件的上传和下载有关系,判断请求的类型是否是multipart类型processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// Determine handler for the current request.//主要看这里,这里是得到HandlerExecutionChain的方法。关于Handler()方法向下看mappedHandler = getHandler(processedRequest);if (mappedHandler == null || mappedHandler.getHandler() == null) {noHandlerFound(processedRequest, response);return;}// Determine handler adapter for the current request.//这里已经获取到HandlerExecutionChain对象,接下来就要获取HandlerAdapter对象,调用Handler对象的方法。HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// Process last-modified header, if supported by the handler//有关浏览器缓存的设定(304)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;}}//pan'duif (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// Actually invoke the 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;}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 afterCompletionif (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {//遍历HandlerMappingList对象(存储若干个HandlerMapping对象),不断调用,直到不为空为止,否则返回nullfor (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;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
想要了解底层的原理必须对请求处理过程和页面的生命周期有点了解才方便您入门学习一下内容:关于请求处理过程和页面的生命周期将会在接下来的日子为大家做一个深入的讲解。
Spring3MVC请求参数获取的几种方法一、通过@PathVariabl获取路径中的参数@RequestMapping(value="user/{id}/{n
在Spring-Cloud-Gateway之请求处理流程文中我们了解最终网关是将请求交给过滤器链表进行处理,接下来我们阅读Spring-Cloud-Gatewa
Spring4.0MVC请求json数据报406错误,如何解决?解决方法一:1、导入jackson-core-2.5.1.jar和jackson-databin
渲染管线一般是由显示芯片GPU内部处理图形信号的并行处理单元组成,这些并行处理单元之间是独立的,从另一个角度看,渲染管线实际上也是一系列绘制过程,这一系列过程的