SPRINGMVC Process Analysis (i)--Overview of the overall process

Source: Internet
Author: User

A general summary of SPRINGMVC

Before also wrote SPRINGMVC process analysis, just then understand not thorough so that article gave up, now better than before, think of writing down to share under, also can enhance the memory, also hope can help others, if there is anything wrong in the article welcome you to point out. (This article is for IT staff with a certain SPRINGMVC experience).

The meaning of 1.springmvc existence

Any framework has its existence value, can more or less help us solve a tedious problem, SPRINGMVC is no exception, in fact, he is nothing great, he is a thoroughly servlet,

A servlet that encapsulates many tasks, and it is these packages that we don't feel he is just a servlet, because he is so powerful, everyone should have the experience of configuring Web. XML when using it, which can clearly see the configuration of Springmvc, as follows:

 <servlet>    <Servlet-name>Springmvc</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    <Init-param>      <Param-name>Contextconfiglocation</Param-name>      <Param-value>/web-inf/classes/spring/spring-servlet.xml</Param-value>    </Init-param> </servlet>  <servlet-mapping>    <Servlet-name>Springmvc</Servlet-name>    <Url-pattern>/</Url-pattern>  </servlet-mapping>

We can see that the configuration of SPRINGMVC is a servlet configuration that intercepts all URLs. So we can summarize the role of SPRINGMVC: As a total servlet, the client's request is distributed, then the different requests are processed and the corresponding data is returned.

Design class diagram for 2.SPRINGMVC

Entrance to the 3.SPRINGMVC

The Dispatcherservlet in the yellow flag is the core entry class for the SPRIGMVC request, and you can see that this class indirectly inherits Httpservletbean through Frameworkservelt, Frameworkservelt implements the Applicationcontextaware, which is used to set the global context of the SPRINGMVC, which enters the Doservice method of Dispatcherservlet when the client sends the request:

  

protected void Doservice (HttpServletRequest request, httpservletresponse response) throws Exception {         //omit unimportant code ...        try {            This.dodispatch (request, response);        } finally {            if (! Webasyncutils.getasyncmanager (Request). isconcurrenthandlingstarted () && attributesSnapshot1! = null) {                This.restoreattributesafterinclude (Request, ATTRIBUTESSNAPSHOT1);}}}    

  actually calls the Dodispatch method, which is very important, and basically all the processing of the request is done in this 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 {try {Modelandview err = null;                Exception dispatchexception = null;                    try {processedrequest = This.checkmultipart (request);                    multipartrequestparsed = processedrequest! = Request;                    Mappedhandler = This.gethandler (processedrequest); if (Mappedhandler = = NULL | | Mappedhandler.gethandler () = = null) {This.nohandlerfound (processedreq                        Uest, response);                    Return              } Handleradapter ex = This.gethandleradapter (Mappedhandler.gethandler ());      String method = Request.getmethod ();                    Boolean isget = "GET". Equals (method); if (Isget | | "HEAD". Equals (method)) {Long lastmodified = ex.getlastmodified (Request, Mappedhandler.gethandler ()                        ); if (this.logger.isDebugEnabled ()) {This.logger.debug ("last                        -modified value for ["+ Getrequesturi (Request) +"] is: "+ lastmodified);                             } if (new Servletwebrequest (request, Response)). Checknotmodified (lastmodified) && isget) {                        Return                        }} if (!mappedhandler.applyprehandle (processedrequest, response)) {                    Return                    } err = Ex.handle (processedrequest, Response, Mappedhandler.gethandler ()); if (asyncmanager.isconcurrenthandlingstarted ()){return;                    } this.applydefaultviewname (request, err);                Mappedhandler.applyposthandle (processedrequest, response, err);                } catch (Exception arg18) {dispatchexception = Arg18;            } this.processdispatchresult (Processedrequest, Response, Mappedhandler, err, dispatchexception); } catch (Exception arg19) {this.triggeraftercompletion (processedrequest, Response, Mappedhandler, Arg19)            ; } catch (Error Arg20) {this.triggeraftercompletionwitherror (processedrequest, Response, Mappedhandler, arg            20); }} finally {if (asyncmanager.isconcurrenthandlingstarted ()) {if (Mappedhandler! = null                ) {mappedhandler.applyafterconcurrenthandlingstarted (processedrequest, response); }} else if (multipartrequestparsed) {THis.cleanupmultipart (processedrequest); }        } }

The first step is to check whether it is a file upload request, (The purpose of this check is to clear the file upload the corresponding information in finally), and then get handler

  Mappedhandler = This.gethandler (processedrequest);

What is handler? In fact, handler is a generic term for the different classes that SPRINGMVC themselves define to handle specific requests, and we know that there are many types of client requests, such as requests for static resources, requests for dynamic resources, restful-style requests, or requests from primitive servlet types, The general name of these processing classes is handler, specifically how to get handler I will explain another chapter. Well, after getting the hander (processing the request Class), are we able to handle the request directly, like this:
Handler.methodname (Request,response);
In fact, this is really true, haha, it is certainly so, but others SPRINGMVC designers than we are more clever, will not be so Lou's writing, well, we see how they are smart it:

  Handleradapter ex = This.gethandleradapter (Mappedhandler.gethandler ());

Handleradapter? What a ghost ah, how to get handler how still get handleradapter, in fact, this is someone else's clever place, here use adapter design mode, each kind of handler will have a corresponding adapter, This way we don't have to judge the type of handler every time, and then call different methods, for example:

Class handler1{
Object handle1 (HttpRequest request,httpresponse response) {
    
}
}
Class handler2{
Object handle2 (HttpRequest request,httpresponse response) {
    
}
}
Class handler3{
Object Handle3 (HttpRequest request,httpresponse response) {
    
}
}

Here are 3 processing classes, normal Our call is this:

if (handle instanceof Handler1) {
Handle.handle1 ();
}
if (handle instanceof Handler2) {
Handle.handle2 ();
}
if (handle instanceof Handler3) {
Handle.handle3 ();
}

If we now have a Handler4 how to do, do we need to change the source code of SPRINGMVC, which obviously does not conform to the design of the open and close principle, adapter mode is the best way to solve this problem each processor must have a corresponding adapter, This allows us to add handler without changing the source code.
Next we see

Err = Ex.handle (processedrequest, Response, Mappedhandler.gethandler ());

This method is the final execution of the request, the different types of handler will have different implementations, when the processor has finished processing their own logic will be put back a modelandview parameter, Modelandview is SPRINGMVC on the processor to return the results of the abstraction, Regardless of what the processor actually returned, SPRINGMVC would say he wrapped it up as a modelandview and then dealt with the Modelandview, which is interesting and instructive, and if we get a lot of different results, We can abstract it into a uniform type of data, and then treat this type of data uniformly, so that we don't have to deal with each case.
Finally, SPINGMVC will process the Modelandview and return it to the browser.

This.processdispatchresult (processedrequest, Response, Mappedhandler, err, dispatchexception);

The above method is the processing of modelandview. At this point, the general flow of SPRINGMVC is over. The above is just a general overview of SPRINGMVC, I will also explain the SPRINGMVC handler,handleradapter, view processing, interceptors, exception handling and other content.









SPRINGMVC Process Analysis (i)--Overview of the overall process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.