Spring MVC is an implementation of an MVC pattern. In the use of spring MVC, you need to configure Dispatcherservlet in Web. XML, which means that its core is a servlet, which implements the front-end controller pattern in Sun's Java EE core mode ( Front Controller), all Web requests need to be processed through it, forwarded, matched, processed, and presented to the page, so this dispatcherservlet can be seen as the most central part of Spring MVC implementation.
Config Dispatcherservlet in Web. xml
<servlet><servlet-name>portrait</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>/web-inf/application-si-portrait-mvc.xml</ Param-value></init-param></servlet>
the context system of spring is established and initialized by Contextloader and Dispatcherservlet . After the initialization of the Contextloaderlistener is complete, the Web container begins initializing the Dispatcherservlet, which is related to the definition of load order in XML. Dispatcherservlet will create its own context to hold the Spring MVC Bean object, and when building this own IOC container, The root context is obtained from the ServletContext as the parent context of the dispatcherservlet holding context. Then initialize the context you hold and finally save the context you hold in ServletContext for later retrieval and use.
The work of Dispatcherservlet can be divided into two parts , one is to initialize the part Initservletbean () to start , The Initstrategies method of Dispatcherservlet is finally called through the Initwebapplicationcontext method, and the other is a servlet that corresponds to the HTTP request. The Web container invokes the servlet's Doget () and Dopost () methods, which are called Doservice after a simple process of Frameworkservlet processrequest (). Finally call the important Dodispatch ().
Spring MVC distribution processing for HTTP requests
After the MVC framework initialization is complete, the processing of HTTP requests is done in the Doservice () method , Dispatcherservlet is a subclass of HttpServlet, as with other HttpServlet, The request to the corresponding HTTP via Doservice (). The processing of the request is actually done by Dodispatch (), which is the primary method for Dispatcherservlet to complete dispatcher, including preparing Modelandview, calling GetHandler to respond to HTTP requests, Then, by performing handler processing to get the returned Modelandview result, the Modelandview object is presented to the corresponding View object.
The implementation of Spring MVC is roughly accomplished by the following steps:
1) establish a mapping relationship between controller controllers and HTTP requests , which is done by Handlerexecutionchain objects encapsulated in handlermapping, The configuration of mappings between the heap controller controllers and the HTTP requests is described in the bean definition and is done by initializing the handlermapping when the IOC container is initialized, and the mappings of these definitions are loaded into a handlermap for use.
2) when initializing to spring MVC to receive HTTP requests and complete the appropriate processing , Dispatcherservlet will request information based on the specific URL when MVC receives the HTTP request. Query in handlermapping to get the corresponding Handlerexecutionchain. The configured controller is encapsulated inside it, and the controller that corresponds to the request completes the requested response action. Generates the required Modelandview object, which, as its name indicates, can get the Modelandview object from the object, from which the model data and view objects are obtained.
3)Dispatcherservlet the obtained model data to a specific view object , which completes the view rendering work of the data.
Spring Technology unveiled----dispatcherservlet