美文网首页SSM
SpringMVC(4): DispatcherServlet前

SpringMVC(4): DispatcherServlet前

作者: 奇点一氪 | 来源:发表于2019-11-25 10:23 被阅读0次

    本文通过前端控制器源码分析springmvc执行过程

    • 1.前端控制器接收请求
      调用doDispatch方法
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HttpServletRequest processedRequest = request;
            HandlerExecutionChain mappedHandler = null;
            boolean multipartRequestParsed = false;
        
             。。。。。
    }
    
    • 2.前端控制器调用HandlerMapping(处理器映射器)根据url查找Handler
    // Determine handler for the current request.
    mappedHandler = getHandler(processedRequest);
    
    /**
         * Return the HandlerExecutionChain for this request.
         * <p>Tries all handler mappings in order.
         * @param request current HTTP request
         * @return the HandlerExecutionChain, or {@code null} if no handler could be found
         */
        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;
        }
    
    
    • 3.调用处理器适配器执行Handler,得到执行的结果ModelAndView mv在doDispatch方法中
    // Actually invoke the handler.
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    
    • 4.视图渲染,将model数据填充到request域
      doDispatch->processDispatchResult->render
      在render方法中,视图解析得到view
    // We need to resolve the view name.
    view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
    

    调用view的渲染方法,将model数据填充到request域

    在render方法中,调用View接口的render方法

    view.render(mv.getModelInternal(), request, response);
    

    相关文章

      网友评论

        本文标题:SpringMVC(4): DispatcherServlet前

        本文链接:https://www.haomeiwen.com/subject/nzyebctx.html