Spring MVC - 请求处理

HttpServletBean

主要参与创建工作,没有涉及请求的处理。

 

 

FrameworkServlet

将所有的请求合并到了processRequest()。

    protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        Throwable failureCause = null;
//获取原来保存的LocalContext,以便后面恢复
        LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
//取得当前请求的LocaleContext
        LocaleContext localeContext = this.buildLocaleContext(request);
//获取原来保存的RequestAttributes,以便后面恢复
        RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
//获取当前请求的LocaleContext
        ServletRequestAttributes requestAttributes = this.buildRequestAttributes(request, response, previousAttributes);
        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
        asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new FrameworkServlet.RequestBindingInterceptor(null));
//设置到静态抽象类的ThreadLocal中
        this.initContextHolders(request, localeContext, requestAttributes);

        try {
//模板方法,DispatcherServlet实现
            this.doService(request, response);
        } catch (ServletException var17) {
            failureCause = var17;
            throw var17;
        } catch (IOException var18) {
            failureCause = var18;
            throw var18;
        } catch (Throwable var19) {
            failureCause = var19;
            throw new NestedServletException("Request processing failed", var19);
        } finally {
//恢复上述内容
            this.resetContextHolders(request, previousLocaleContext, previousAttributes);
            if(requestAttributes != null) {
                requestAttributes.requestCompleted();
            }

            if(this.logger.isDebugEnabled()) {
                if(failureCause != null) {
                    this.logger.debug("Could not complete request", (Throwable)failureCause);
                } else if(asyncManager.isConcurrentHandlingStarted()) {
                    this.logger.debug("Leaving response open for concurrent processing");
                } else {
                    this.logger.debug("Successfully completed request");
                }
            }

            this.publishRequestHandledEvent(request, response, startTime, (Throwable)failureCause);
        }

    }

this.initContextHolders(request, localeContext, requestAttributes); 将LocaleContext和RequestAttributes设置到静态抽象类的ThreadLocal属性中,可通过LocaleContext.getLocaleContext()在service层获得Locale信息,RequestAttributes同理。可以通过getRequest、getSession、getResponse,在任何地方都能方便的取得

最后发布消息,可通过实现ApplicationListener<ServletRequestHandleEvent>,并在类上标注@Component就可以了。

 

 

DispatcherServlet

请求处理入口是doService()。

    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(this.logger.isDebugEnabled()) {
            String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult()?" resumed":"";
            this.logger.debug("DispatcherServlet with name '" + this.getServletName() + "'" + resumed + " processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]");
        }

        Map<String, Object> attributesSnapshot = null;
//如果是include请求,则对request的attribute做快照
        if(WebUtils.isIncludeRequest(request)) {
            attributesSnapshot = new HashMap();
            Enumeration attrNames = request.getAttributeNames();

            label108:
            while(true) {
                String attrName;
                do {
                    if(!attrNames.hasMoreElements()) {
                        break label108;
                    }

                    attrName = (String)attrNames.nextElement();
                } while(!this.cleanupAfterInclude && !attrName.startsWith("org.springframework.web.servlet"));

                attributesSnapshot.put(attrName, request.getAttribute(attrName));
            }
        }
//设置request属性
//前四个属性在handler和view中使用。
        request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.getWebApplicationContext());
        request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
        request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
        request.setAttribute(THEME_SOURCE_ATTRIBUTE, this.getThemeSource());
//后三个属性与flashMap有关。redirect传递flashMap传递参数。
        FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
        if(inputFlashMap != null) {
            request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
        }

        request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
        request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);

        try {
            this.doDispatch(request, response);
        } finally {
//还原上述快照
            if(!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {
                this.restoreAttributesAfterInclude(request, attributesSnapshot);
            }

        }

    }

redirect时可通过向handler的方法中传入RedirectAttributes类型的参数,把需要的属性设置到里面

而后有交给了doDispatcher()。

 

 

doDispatcher()

    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpServletRequest processedRequest = request;
        HandlerExecutionChain mappedHandler = null;
        boolean multipartRequestParsed = false;
        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

        try {
            try {
                ModelAndView mv = null;
                Exception dispatchException = null;

                try {
//检查是不是上传请求
                    processedRequest = this.checkMultipart(request);
                    multipartRequestParsed = processedRequest != request;
//通过request找到handler
                    mappedHandler = this.getHandler(processedRequest);
                    if(mappedHandler == null || mappedHandler.getHandler() == null) {
                        this.noHandlerFound(processedRequest, response);
                        return;
                    }

//通过handler找到handlerAdapter
                    HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
                    String method = request.getMethod();
//处理GET、HEAD请求的Last-Modified
                    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()
                    if(!mappedHandler.applyPreHandle(processedRequest, response)) {
                        return;
                    }
//通handlerAdapter处理handler
                    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//如果需要异步,直接返回
                    if(asyncManager.isConcurrentHandlingStarted()) {
                        return;
                    }
//View返回空时,根据request设置默认View
                    this.applyDefaultViewName(processedRequest, mv);
//执行Interceptor的postHandle()
                    mappedHandler.applyPostHandle(processedRequest, response, mv);
                } catch (Exception var19) {
                    dispatchException = var19;
                }
//处理上面的结果。包括异常处理、渲染页面、触发Interceptor的afterCompletion
                this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
            } catch (Exception var20) {
                this.triggerAfterCompletion(processedRequest, response, mappedHandler, var20);
            } catch (Error var21) {
                this.triggerAfterCompletionWithError(processedRequest, response, mappedHandler, var21);
            }

        } finally {
            if(asyncManager.isConcurrentHandlingStarted()) {
                if(mappedHandler != null) {
                    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
                }
            } else if(multipartRequestParsed) {
                this.cleanupMultipart(processedRequest);
            }

        }
    }
  • Handler:具体处理请求的处理器,对应Controller层,表现形式有很多种,可以是类,也可以是方法,类型是Object,标注了@RequestMapping的都可以看作handler。
  • HandlerMapping:用来查找Handler。
  • HandlerAdapter:Handler可以是任意形式的,而Servlet需要处理的方法结构是固定的。adapter让固定的Servlet调用灵活的Handler。

View和ViewResolver的原理与Handler和HandlerMapping类似。View用于展示数据,ViewResolver用来查找View,Model是View要展示的数据。

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3171491/blog/1523447

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值