Spring WebMVC executes the Controller process

The first article learned that Spring Web MVC is the original web framework built on the Servlet API.

The core processor DispatcherServlet ultimately inherits the parent class HttpServlet. So all requests will enter the call chain of the doGet or doPost method.

 Let’s look at the first concept HandlerMapping. There are two here. This should be found in the previous article. Handler (can be understood as the corresponding Controller object)

1.@Controller annotation is used HandlerMapping is RequestMappingHandlerMapping

2. The HandlerMapping that implements the interface is BeanNameUrlHandlerMapping

 The call chain of doGet and doPost ends here

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

    try {
        try {
            ModelAndView mv = null;
            Object dispatchException = null;

            try {
                processedRequest = this.checkMultipart(request);
                multipartRequestParsed = processedRequest != request;
                mappedHandler = this.getHandler(processedRequest);//1.通过url 获取对应的handler 对象 包含了要执行的方法
                if (mappedHandler == null) { 
                    this.noHandlerFound(processedRequest, response); 
                    return; 
                } 

                HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());//2. Obtain the adapter that executes the method through the handler object, which is the controller Object (the implementation of a method requires instance object.method and target parameters) 
                String method = request.getMethod(); 
                boolean isGet = "GET".equals(method); 
                if (isGet || "HEAD".equals(method)) { 
                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); 
                    if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) { 
                        return; 
                    } 
                } 

                if (!mappedHandler.applyPreHandle(processedRequest, response)) {//Call the interceptor
                    return;
                }

                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());//3.执行方法
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }

                this.applyDefaultViewName(processedRequest, mv);
                mappedHandler.applyPostHandle(processedRequest, response, mv);
            } 

            this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
        }  

    } finally {
        

    }
}

 1. Obtaining the corresponding handler object through the url contains the method to be executed

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    if (this.handlerMappings != null) {
        Iterator var2 = this.handlerMappings.iterator();

        while(var2.hasNext()) {
            HandlerMapping mapping = (HandlerMapping)var2.next();
            //处理注解类的,此处的HandlerMapping == RequestMappingHandlerMapping handler才不为空
            HandlerExecutionChain handler = mapping.getHandler(request); 
            if (handler != null) {
                return handler;
            }
        }
    }

    return null;
}

 

public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    Object handler = this.getHandlerInternal(request);//通过url 进行匹配
    if (handler == null) {
        handler = this.getDefaultHandler();
    }

    if (handler == null) {
        return null;
    } else {
        if (handler instanceof String) {
            String handlerName = (String)handler;
            handler = this.obtainApplicationContext().getBean(handlerName);
        }

        HandlerExecutionChain executionChain = this.getHandlerExecutionChain(handler, request);//最后封装返回
        //有删减
        return executionChain;
    }
}
//通过url 进行匹配
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    String lookupPath = this.getUrlPathHelper().getLookupPathForRequest(request);//获取路径
    this.mappingRegistry.acquireReadLock();

    HandlerMethod var4;
    try {
        HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);//获取方法
        var4 = handlerMethod != null ? handlerMethod.createWithResolvedBean() : null;
    } finally {
        this.mappingRegistry.releaseReadLock();
    }

    return var4;
}

//2. Obtain the adapter that executes the method through the handler object, that is, the controller object (implementation of a method requires instance object.method and target parameters)

There are 3 types of adapters

1.RequestMappingHandlerAdapter - annotation registered @controller adapter - execution through reflection method we mainly look at this

2.HttpRequestHandlerAdapter - implements the controller adapter registered by the HttpRequestHandler interface - directly calls the overridden method execution

3.SimpleControllerHandlerAdapter - the controller adapter that implements the controller interface registration - directly call the overridden method execution

//3. Execution method

Guess you like

Origin blog.csdn.net/qq_38108719/article/details/103443255