The execution process of springmvc in tomcat

1. When the WEB container is started, it will create a corresponding ServletContext object for each WEB application ( each web application is unique ), which represents the current web application web container to provide a global context environment, which is the latter The spring IoC container provides the hosting environment;

Second, read web.xml

The server software or container such as (tomcat) loads the web.xml file in the project, and starts the project through various configurations in it. The project can be started correctly only when the configuration items are correct. web.xml has a number of tags, and the order in the loading process is: context-param >> listener >> fileter >> servlet​. (Multiple nodes of the same type are loaded in order of appearance)

1. After the ServletContext is created, the initialization WebApplicationContext event is triggered by starting the WebApplicationContext's servlet (org.springframework.web.context.ContextLoaderServlet) or the Web container listener (org.springframework.web.context.ContextLoaderListener) . This is the spring's ioc container; (For the initialization process of application context, please refer to: http://www.cnblogs.com/hantalk/p/6647772.html)

http://images2015.cnblogs.com/blog/1136989/201704/1136989-20170401015757961-1880140446.png

2. The container will read the context-param (there is no context-param in springmvc, that is, the WebApplicationContext does not need to configure specific data), and spring uses WebApplicationContext.ROOT WEB APPLICATION CONTEXT ATTRIBUTE as the attribute Key, and context-param as the attribute value pair and save it servletContext ;

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);

E.g:

[html]  view plain copy  
  1. <context-param>  
  2.       <param-name>contextConfigLocation</param-name>  
  3.       <param-value>  
  4.           classpath:spring-common-config.xml,  
  5.           classpath:spring-budget-config.xml  
  6.       </param-value>  
  7. </context-param>  

3. Read the listener to create a listener, and instantiate the filter filter according to the specified classpath ;

4. Initialize dispatcherservlet in web.xml

First, use WebApplicationContext.ROOT WEB APPLICATION CONTEXT ATTRIBUTE to obtain the previous WebApplicationContext from the ServletContext as the parent context of its own context.

Then, read the /WEB-INF/[servlet name]-servlet.xml file and initialize the context held by itself. After initialization, spring also stores the context of dispatcherservlet in ServletContext.

DispatcherServlet is mainly used for responsibility scheduling, and itself is mainly used to control the process. Read the following code, you can see the main responsibilities in the initStrategies method as follows:

1. File upload parsing, if the request type is multipart, the file upload parsing will be performed through MultipartResolver;

2. Through HandlerMapping, map the request to the processor (return a HandlerExecutionChain, which includes a processor and multiple HandlerInterceptor interceptors);

3. Support multiple types of processors (processors in HandlerExecutionChain) through HandlerAdapter;

4. Parse the logical view name to the specific view through ViewResolver;

5. Localization analysis;

6. Rendering specific views, etc.;

7. If an exception is encountered during execution, it will be handed over to HandlerExceptionResolver for resolution.

[java]  view plain copy  
  1. publicclass DispatcherServlet extends FrameworkServlet {   
  2.      // Implement the onRefresh() method of the subclass, which is delegated to the initStrategies() method.  
  3.     @Override  
  4.     protectedvoid onRefresh(ApplicationContext context) {   
  5.        initStrategies(context);  
  6.     }  
  7.    
  8.     //Initialize the strategy used by the default Spring Web MVC framework (such as HandlerMapping)  
  9.     protectedvoid initStrategies(ApplicationContext context) {   
  10.        initMultipartResolver(context);  
  11.        initLocaleResolver(context);  
  12.        initThemeResolver(context);  
  13.        initHandlerMappings(context);  
  14.        initHandlerAdapters(context);  
  15.        initHandlerExceptionResolvers(context);  
  16.        initRequestToViewNameTranslator(context);  
  17.        initViewResolvers(context);  
  18.        initFlashMapManager(context);  
  19.     }  
  20. }  

3. Specific request processing

1. First, the user sends a request http://localhost:9080/springmvc-chapter2/hello to the web container, and the web container maps it to the DispatcherServlet (url-pattern is /) according to the "/hello" path for processing;

2. When a request is received, the dispatcherservlet passes the request to handlerMapping , and lets it find the handlerExecutionChain object corresponding to the request, and the handlerExecutionChain returns the interceptor and handler . handlerExecutionChain is an execution chain, which includes a handler (handler, which is the xxxController in the code ) that handles the request, and may also include several handlerInterceptors (interceptors) that intercept the request;

3. SimpleController HandlerAdapter adapts the handler ( xxxController ) in HandlerExecutionChain to SimpleControllerHandlerAdapter ;

4. The SimpleController HandlerAdapter will call the specific method in xxxController for functional processing . The processing method returns a ModelAndView to the DispatcherServlet ; the handlerAdapter has three external methods API:

[java]  view plain copy  
  1. //Determine whether this adapter can handle this kind of handler  
  2.   
  3. boolean supports(Object handler);  
  4.   
  5. //Encapsulate the logic of handler processing request  
  6.   
  7. ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;  
  8.   
  9. long getLastModified(HttpServletRequest request, Object handler);  

Handlermapping is to find the corresponding processing object for the request, no matter what method is used to process it. And handlerAdapter is to call specific methods to process requests.

5. The dispatherServlet passes the data of the modelAndview in the controller to the front-end template freemarker/velocity through the ViewResolver in the configuration file ;

6. Front-end template rendering , displaying the model data passed in from the processor in the view ;

7. Return the control to the DispatcherServlet, and the DispatcherServlet returns the response to the user. At this point, the process ends.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325192650&siteId=291194637