SpringMVC源代码学习(四)九大组件上

1. HandlerMapping

在dispatcherServlet,doDispatch方法中有调用getHandler,代码如下: 
其中List< HandlerMapping> handlerMappings是dispatcherServlet的内部变量。 
那该方法的内容就是遍历handlerMappings,获得符合条件的HandlerMapping,调用其getHandler方法,返回获得的HandlerExecutionChain

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

HandlerMapping是一个接口,内部只有一个方法和诺干变量,它的作用是根据request找到对应的Handler。方法如下: 
HandlerExecutionChain getHandler(HttpSevletRequest request) throws Exception 
接下来看看一个该方法的实现,SimpleControllerHandlerAdapter,代码如下:

    public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        Object handler = getHandlerInternal(request);
        if (handler == null) {
            handler = getDefaultHandler();
        }
        if (handler == null) {
            return null;
        }
        // Bean name or resolved handler?
        if (handler instanceof String) {
            String handlerName = (String) handler;
            handler = getApplicationContext().getBean(handlerName);
        }

        HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
        if (CorsUtils.isCorsRequest(request)) {
            CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
            CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
            CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
            executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
        }
        return executionChain;
    }

另外一个要讨论的就是顺序问题,不同hander负责映射的条件可能有重复的,这时候就需要定义不同的HandlerMapping执行的顺序,这里的顺序可以通过实现Order接口,通过Order属性定义。order越小越先使用。如:

<bean class="com.excelib.TudouHandlerMapping"
    p:order="1"/>
<bean class="com.excelib.TudoupianHandlerMapping"
    p:order="0"/>

2.HandlerAdapter

在dispatcherServlet通过如下方法获得HandlerAdapter,其中List< HandlerAdapter>handlerAdapters是dispatcherServlet的成员变量,可以看到它的逻辑是遍历所有的Adapter,然后检查哪个可以处理当前的Handler,找到第一个可以处理Handler的Adapter后停止查找,返回。这里的顺序同样是通过Order属性设置的。

    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
        for (HandlerAdapter ha : this.handlerAdapters) {
            if (logger.isTraceEnabled()) {
                logger.trace("Testing handler adapter [" + ha + "]");
            }
            if (ha.supports(handler)) {
                return ha;
            }
        }
        throw new ServletException("No adapter for handler [" + handler +
                "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
    }

上篇文章有介绍,HandlerAdapter的角色是使用工具(handler)的人。因为handler是Object类型,需要HandlerAdapter使用它来完成一定格式要求的任务。 
它是一个接口,代码如下:

    public interface HandlerAdapter {   
        boolean supports(Object handler);   
        ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
       long getLastModified(HttpServletRequest request, Object handler);

接下来看一个spring自己实现的示例,代码如下:

public class SimpleControllerHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        return (handler instanceof Controller);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        return ((Controller) handler).handleRequest(request, response);
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
        if (handler instanceof LastModified) {
            return ((LastModified) handler).getLastModified(request);
        }
        return -1L;
    }

}

可以看到这个Adapter比较简单,它要求handler实现了Controller接口,方法的实现是通过处理器的handleRequest方法。

3. HandlerExceptionResolver

HandlerExceptionResolver是SpringMVC中专门负责处理异常的类。它负责: 
根据异常设置ModelAndView。 
之后交给render方法进行渲染。render只负责将Model渲染成页面。具体ModelAndView的来源render并不关心。

public interface HandlerExceptionResolver {
    ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);

}

它的结构很简单,只有一个方法,只需要从异常解析出ModelAndView就可以了。具体实现可以维护一个异常为Key、View为value的Map,解析时直接从Map里获取View。

4.ViewResolver

ViewResolver用来将String类型的视图名(也叫逻辑视图)和Locale解析为View类型的视图,ViewResolver接口也很简单,代码如下

public interface ViewResolver {
    View resolveViewName(String viewName, Locale locale) throws Exception;

}

这里可以看到参数是viewName和locale,不过一般我们只要根据视图名找到视图,然后渲染就可以,如果需要国际化支持也只要将显示的内容或者主题使用国际化支持。 
View是用来渲染页面的,也就是将程序返回的参数填入模板中,生成html或其他格式的文件。所以说它会解决两个问题: 
1. 填入哪个模板? 
2. 如何填入? 
它会找到渲染所用的模板和所用的技术(也就是视图的类型)进行渲染,具体的渲染过程则交给不同的视图自己完成。 
我们最常用的UrlBasedViewResolver系列的解析器都是针对单一视图类型进行解析的。比如InternalResourceViewResolver只针对jsp类型的视图,FreeMarkerViewResolver只针对FreeMarker,VelocityViewResolver只针对Velocity。而ResourceBundleViewResolver、XmlViewResolver、BeanNameViewResolver等解析器可以同时解析多种类型的视图。 
ResourceBundleViewResolver使用properties配置文件来进行配置解析的文件类和url、XmlViewResolver使用xml配置。BeanNameViewResolver是根据ViewName从ApplicationContext容器中查找相应的bean做View的,它比较简单,源码如下:

public class BeanNameViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered {
    private int order = Integer.MAX_VALUE;  // default: same as non-Ordered
    public void setOrder(int order) {
        this.order = order;
    }
    @Override
    public int getOrder() {
        return this.order;
    }
    @Override
    public View resolveViewName(String viewName, Locale locale) throws BeansException {
        ApplicationContext context = getApplicationContext();
        if (!context.containsBean(viewName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("No matching bean found for view name '" + viewName + "'");
            }
            // Allow for ViewResolver chaining...
            return null;
        }
        if (!context.isTypeMatch(viewName, View.class)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found matching bean for view name '" + viewName +
                        "' - to be ignored since it does not implement View");
            }
            // Since we're looking into the general ApplicationContext here,
            // let's accept this as a non-match and allow for chaining as well...
            return null;
        }
        return context.getBean(viewName, View.class);
    }

}

可以看出原理就是根据viewName从spring容器中查找Bean,if(找不到||不是view类型) return null; else return context.getBean(viewName, View.class); 
ViewResolver的使用需要注册到Spring MVC容器中,默认使用的是org.springframework.web.servlet.view.InternalResourceViewResolver。

 
0
posted @ 2017-08-17 17:01  0101无限的空间  阅读(355)  评论(0编辑  收藏  举报