设为首页 加入收藏

TOP

Spring入门进阶之DispatcherServlet源码分析(五)
2018-01-17 13:05:27 】 浏览:955
Tags:Spring 入门 进阶 DispatcherServlet 源码 分析
());


            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 afterCompletion
            if (mappedHandler != null) {
                mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
            }
        }
        else {
            // Clean up any resources used by a multipart request.
            if (multipartRequestParsed) {
                cleanupMultipart(processedRequest);
            }
        }
    }
}


先来看看Spring中一个简单的映射处理器配置:


<bean id="simpleUrlMapping"
    class="org.Springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/test.html">controller</prop>
        </props>
    </property>
</bean>


在Spring的加载中,会将类型为SimpleUrlHandlerMapping的实例加载到this.handlerMappings中,根据request提取对应的Handler,也就是提取当前实例的controller,这里的controller是继承自AbstractController类型实例,看看这步是如何封装的,跟进getHandler方法源码:


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;
}


在系统启动时Spring会将映射类型的bean注册到this.handlerMappings变量中,此方法的目的就是遍历所有的HandlerMapping,并调用其getHandler方法进

首页 上一页 2 3 4 5 6 7 8 下一页 尾页 5/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java中BigDecimal的基本运算 下一篇Java静态代码块使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目