First, the main components of SPRINGMVC
- Front-end Controller (disatcherservlet): receives a request, responds to a result, returns a data type that can be json,string, or it can be a page (Model).
- Processor Mapper (handlermapping): to find a processor based on a URL, typically through an XML configuration or annotation.
- Processor (Handler): That's what we often call controller controllers, written by programmers.
- Processor Adapter (handleradapter): The processor can be packaged as an adapter so that multiple types of processors can be supported.
- View Resolver (Viewresovler): for view resolution, return to the View object (common Jsp,freemark, etc.).
Second, the working principle of SPINGMVC
Step Description:
1, the user sends the request to the front controller (dispatcherservlet).
2. The front controller requests the Processor mapper (handlermapping) to find the processor (Handler).
3. When found, the processor mapper (handlermappering) returns the execution chain (Handlerexecutionchain) to the front-end controller.
4. The front controller (Dispatcherservlet) calls the processor adapter (Handleradapter) to execute the processor (Handler).
5, processor adapter to perform handler.
6, the processor finishes executing to the processor adapter return Modelandview.
7. The processor adapter returns Modelandview to the front-end controller.
8, the front controller requests the View resolver (viewresolver) to do the view resolution.
9. The view resolver returns to the front controller.
10. The front controller renders the view.
11, the front-end controller to the user response results.
Three, the realization principle
The Dispatcherservlet action can be roughly divided into two parts: the initialization part and the response to the HTTP request
1. Initialize:
Httppservletbean.init ()---->frameworkservlet.initservletbean ()
---->initwebapplicationcontext ()
---->dispatcherservlet.onrefresh ()
---->initstrategies ()
protected void Initstrategies (ApplicationContext context) {//Initialize Multimedia parser initmultipartresolver (context); Initializes the position resolver initlocaleresolver (context); Initializes the theme resolver initthemeresolver (context); Initializes the Handlermappings inithandlermappings (context); Initializes the Handleradapters inithandleradapters (context); Initializes the exception resolver inithandlerexceptionresolvers (context); Initializes the request to the View name Converter initrequesttoviewnametranslator (context); Initializes the View resolver initviewresolvers (context); Initializes the Flashmapmanager Initflashmapmanager (context); }
2. Responding to HTTP requests
The Doget () and Dopost () methods of the servlet are invoked as a servlet,web container, after the Frameservlet ProcessRequest () is simply processed, The Doservice () method of Dispatcherservlet is called, and Dodispatch () is encapsulated in this method.
Dodispatch method
protected void Dodispatch (HttpServletRequest request, httpservletresponse response) throws Exception {Httpservletr Equest 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); Determine handler for the current request. Mappedhandler = GetHandler (processedrequest); Find handler and its interceptor if (Mappedhandler = = NULL | | Mappedhandler.gethandler () = = null) {Noha Ndlerfound (processedrequest, response); Return }//Determine handler adapter for the current request. HandleradaptEr ha = gethandleradapter (Mappedhandler.gethandler ()); Find the handler adapter//Process last-modified header, if supported by the handler. String method = Request.getmethod (); Boolean isget = "GET". Equals (method); if (Isget | | "HEAD". Equals (method)) {Long lastmodified = ha.getlastmodified (Request, Mappedhandler.gethandler ()); if (logger.isdebugenabled ()) {Logger.debug ("last-modified value for [" + getrequ Esturi (Request) + "] is:" + lastmodified); } if (new Servletwebrequest (request, Response). Checknotmodified (lastmodified) && isget) { Return }} if (!mappedhandler.applyprehandle (processedrequest, Response)) {Retu Rn }//actually invoke the handler. MV = Ha.handle (Processedrequest, ResponSE, Mappedhandler.gethandler ()); Executes handler through the adapter and returns the Modelandview object if (asyncmanager.isconcurrenthandlingstarted ()) {RET Urn } 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 W ell,//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 an D aftercompletion if (mappedhandler! = null) {Mappedhandler.applyafterconcurrenthandlin gstarted (processedrequest, response); }} else {//clean up any of the resources used by a multipart request. if (multipartrequestparsed) {Cleanupmultipart (processedrequest); } } } }
Properties of the Handlerexecutionchain
Public class Handlerexecutionchain { private static final Log logger = Logfactory.getlog ( Handlerexecutionchain.class); Private final Object handler; Private handlerinterceptor[] interceptors; Private List<handlerinterceptor> interceptorlist; private int interceptorindex =-1;
}
Ref: 77619512
SPRINGMVC Working principle