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的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM