SpringMVC处理请求

HttpServletBean浏览器

HttpServletBean主要参与了建立工做,并无涉及请求的处理。缓存

FrameworkServlet服务器

FrameworkServlet的service方法里添加了对PATCH的处理,并将全部须要本身处理的请求都集中到了processRequest方法进行统一处理,这和HttpServlet里面根据request的类型将请求分配到各个不一样的方法进行处理的过程正好相反。
processRequest方法里主要的处理逻辑交给了doService,这是一个模板方法,在子类DispatcherServlet实现。app

DispatcherServlet异步

DispatcherServlet的doServic并无直接进行处理,而是交给了doDispatch进行具体的处理;在doDispatch处理前doServic作了一些事情,判断是否是include请求,若是是则对request的Attribute作个快照备份,等doDispatch处理完以后进行还原。async

doDispatch的核心代码
// 根据request找到Handler
mappedHandler = getHandler(processedRequest);
// 根据Handler找到对应的HandlerAdapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// 用HandlerAdapter处理Handler
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// 处理上面的结果,包含找到View并渲染输出给用户
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);源码分析

Handler:处理器,对应MVC中的Controller层,它能够是类,也能够是方法;标注了@RequestMapping的方法就是一个Handler。
HandlerMapping:用来查找Handler
HandlerAdapter:Spring MVC中的Handler能够是任意的形式,只要能处理请求就OK,可是Servlet须要的处理方法的结构倒是固定的,都是以request和response为参数的方法。HandlerAdapter让固定的Servlet处理方法能够调用灵活的Handler来处理请求。post

源码分析:this

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 请求对象,若是是上传请求会封装为上传类型的request
    HttpServletRequest processedRequest = request;
    // 处理器链,包含处理器和Interceptor
    HandlerExecutionChain mappedHandler = null;
    // 是否是文件上传
    boolean multipartRequestParsed = false;
    // 异步管理
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

    try {
        try {
            ModelAndView mv = null;
            // 异常对象
            Object dispatchException = null;
            try {    
                //若是是上传请求,将request转换为MultipartHttpServletRequest,用到了MultipartResolver
                processedRequest = this.checkMultipart(request);
                multipartRequestParsed = processedRequest != request;
                // 根据request找处处理器链,其中包含与当前request相匹配的Interceptor和handler
                // Interceptor和Handler,执行时先调用Interceptor的preHandle方法,最后执行Handler
                // 返回的时候按相反的顺序执行Interceptor的postHandle方法
                mappedHandler = this.getHandler(processedRequest);
                if (mappedHandler == null || mappedHandler.getHandler() == null) {
                    this.noHandlerFound(processedRequest, response);
                    return;
                }
                // 根据Handler找到对应的HandlerAdapter
                HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
                /* 处理Get、Head请求的LastModified
                 * 当浏览器第一次跟服务器请求资源(GET、Head请求)时,
                 * 服务器在返回的请求头里面会包含一个Last-Modified的属性,
                 * 表明本资源最后是何时修改的。
                 * 在浏览器之后发送请求时会同时发送以前接收到的LastModified,
                 * 服务器接收到带Last-Modified的请求后会用其值和本身实际资源的最后修改时间作对比,
                 * 若是资源过时了则返回新的资源(同时返回新的Last-Modified),
                 * 不然直接返回304状态码表示资源未过时,浏览器直接使用以前缓存的结果。
                 */
                String method = request.getMethod();
                boolean isGet = "GET".equals(method);
                if (isGet || "HEAD".equals(method)) {
                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
                    }

                    if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }
                /* 接下来依次调用相应Interceptor的preHandle、
                 * HandlerAdapter使用Handler处理请求,Controller就是在这个地方执行的,
                 * Handler处理完请求后,若是须要异步处理,则直接返回,
                 * 若是不须要异步处理,当view为空时(如Handler返回值为void),
                 * 设置默认view,而后执行相应Interceptor的postHandle。
                 */ 
                // 执行相应的Interceptor的preHandle 
                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }
                // HandlerAdapter使用Handler处理请求
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                // 若是须要异步处理直接返回
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }
                // 当view为空时,好比,Handler返回值为void,根据request设置默认的view
                this.applyDefaultViewName(processedRequest, mv);
                // 执行相应Interceptor的方法
                mappedHandler.applyPostHandle(processedRequest, response, mv);
            } catch (Exception var20) {
                dispatchException = var20;
            } catch (Throwable var21) {
                dispatchException = new NestedServletException("Handler dispatch failed", var21);
            }
            // 处理返回结果。包括处理异常、渲染页面、发出完成通知触发Interceptor的afterCompletion
            this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
        } catch (Exception var22) {
            this.triggerAfterCompletion(processedRequest, response, mappedHandler, var22);
        } catch (Throwable var23) {
            this.triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", var23));
        }

    } finally {
        // 判断是否执行异步请求
        if (asyncManager.isConcurrentHandlingStarted()) {
            if (mappedHandler != null) {
                mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
            }
        // 删除上传请求的资源    
        } else if (multipartRequestParsed) {
            this.cleanupMultipart(processedRequest);
        }

    }
}
相关文章
相关标签/搜索