SpringMVC中的handler是什么?什么类型?

从DispatcherServlet中开始找,首先要知道hander是Object类型,一开始是一个类的全限定名。

HandlerExecutionChain handler = hm.getHandler(request);
//是由HandlerMapping得到的: HandlerMapping.getHandler(HttpServletRequest request)
handler = getApplicationContext().getBean(handlerName);
//在HandlerMapping的getHandler中: AbstractHandlerMapping.getHandler(HttpServletRequest request)

 而if(handler instanceof String) String handlerName = (String) handler; 应该是一个全限定名。

 得到handler、  再下来得到HandlerExecutionChain,就可以返回到DispatcherServlet了

HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);

 

HandlerExecutionChain的构造方法:
public HandlerExecutionChain(Object handler) {
		this(handler, (HandlerInterceptor[]) null);
	}

 到这里就可以return HandlerExecutionChain了。

还没弄清楚String handler是哪里冒出来的。

public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
		Object handler = getHandlerInternal(request);  
//就是这里拿到的,在本类中是abstract方法

 

    AbstractUrlHandlerMapping是一个实现getHandlerInternal(request)方法的abstract类。
      且返回类型Object,像是我们要找的.

/**
	 * Look up a handler for the URL path of the given request.
	 * @param request current HTTP request
	 * @return the handler instance, or {@code null} if none found
      */

 下面是AbstractUrlHandlerMapping得到handler的方法实现:

protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
    // 这个lookupPath应该叫做path或者urlPath。写出来为了易读。
        String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); 
        Object handler = lookupHandler(lookupPath, request);  //lookup有查表的意思。
        if (handler == null) {
            // We need to care for the default handler directly, since we need to
            // expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well.
            Object rawHandler = null;
            if ("/".equals(lookupPath)) {
            ……

 

忽然发现上面的都是废话,我就想知道怎么从request的url中得到handler的。

posted @ 2019-10-28 17:01  梧桐将秋色  阅读(4639)  评论(0编辑  收藏  举报