java source-springmvc

<img

https://www.cnblogs.com/Java-Starter/p/10310565.html

Initialization process

https://blog.csdn.net/dhaiuda/article/details/80026354

Summarize the initialization process of HttpServletBean, FrameworkServlet and DispatcherServlet

1.HttpServletBean

Initialize the parameters in web.xml

2.FrameworkServlet

Give context to current Servlet

3.DispatcherServlet

Initialize HandlerMapping (Request Mapping), HandlerExceptionResolver (Exception Handling), ViewResolver (View Resolution) and other function implementation classes

img

DispatcherServlet handles the request

Enter http: // localhost: 8080 / springmvcdemo / employee in the browser to trigger the processRequest method of DispatcherServlet

First look at the processRequest method of the parent class FrameworkServlet:

previousLocaleContext Get the LocaleContext related to the current thread

Construct a new LocaleContext related to the current thread according to the existing request

previousAttributes Get RequestAttributes bound to the current thread

Construct new ServletRequestAttributes for existing requests and add pre-bound attributes

initContextHolders binds the newly constructed RequestAttributes and ServletRequestAttributes to the current thread, joins to ThreadLocal to complete the binding

Abstract method doService is overridden by FrameworkServlet subclass DispatcherServlet

img

img

The resetContextHolders method unbinds RequestAttributes, ServletRequestAttributes and the current thread

Register to listen to the event ServletRequestHandledEvent, and generate an Event when calling the context

Now we look at the doService method of DispatcherServlet

img

attributesSnapshot is used to save the data in the request field, which can be called "snapshot"

img

Enter doDispatch method

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

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

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

			try {
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				
                //通过当前request获取到 HandlerExecutionChain
				mappedHandler = getHandler(processedRequest);
				if (mappedHandler == null) {
					noHandlerFound(processedRequest, response);
					return;
				}

				
                //确定当前请求的处理程序适配器。
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

				// 如果处理程序支持,则处理最后修改的标头
				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)) {
					return;
				}

				// 实际调用处理程序。
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

				if (asyncManager.isConcurrentHandlingStarted()) {
					return;
				}

				applyDefaultViewName(processedRequest, mv);
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			catch (Exception ex) {
				dispatchException = ex;
			}
			catch (Throwable err) {
				// As of 4.3, we're processing Errors thrown from handler methods as well,
				// making them available for @ExceptionHandler methods and other scenarios.
				dispatchException = new NestedServletException("Handler dispatch failed", err);
			}
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}
		catch (Exception ex) {
			triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
		}
		catch (Throwable err) {
			triggerAfterCompletion(processedRequest, response, mappedHandler,
					new NestedServletException("Handler processing failed", 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);
				}
			}
		}
	}
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
   if (this.handlerAdapters != null) {
      for (HandlerAdapter adapter : this.handlerAdapters) {
         if (adapter.supports(handler)) {
            return adapter;
         }
      }
   }
   throw new ServletException("No adapter for handler [" + handler +
         "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}

The first one is: Find the appropriate controller from the two handlers

BeanNameUrlHandlerMapping @ 833153a, XML configuration mode controller

RequestMappingHandlerMapping @ 3602f818 Annotated controller

The second is: from the three adapters, find the right processor

HttpRequestHandlerAdapter @ 28dc72d7, SimpleControllerHandlerAdapter @ 6b712691, Adapter and annotation of XML configuration

RequestMappingHandlerAdapter @ 4d763f47 Annotated Adapter

https://blog.csdn.net/abcwanglinyong/article/details/91044492

img

1. The user sends a request, which is intercepted by SpringMVC's front-end controller DispatcherServlet.

2. DispatcherServlet calls the HandlerMapping processor mapper after receiving the request.

3. The processor mapper finds the specific processor, generates a processor execution chain HandlerExecutionChain (including the processor Handler and processor interceptor collection), and returns it to the DispatcherServlet.

4. DispatcherServlet calls the HandlerAdapter processor adapter.

5. Find the specific Controller through the HandlerAdapter

6. The Controller encapsulates the model data Model to ModelAndView and returns it to the HandlerAdapter.

7. The HandlerAdapter returns the model view ModelAndView to the DispatcherServlet.

8. DispatcherServlet passes ModelAndView to the ViewReslover view parser.

9. ViewReslover returns specific View after parsing.

10. DispatcherServlet renders the view according to the View (that is, fills the model data into the view).

11. DispatcherServlet responds the view to the user.
To DispatcherServlet.

8. DispatcherServlet passes ModelAndView to the ViewReslover view parser.

9. ViewReslover returns specific View after parsing.

10. DispatcherServlet renders the view according to the View (that is, fills the model data into the view).

11. DispatcherServlet responds the view to the user.

Published 37 original articles · praised 6 · visits 4651

Guess you like

Origin blog.csdn.net/littlewhitevg/article/details/105475071