001 SPRINGMVC Overview

Source: Internet
Author: User
Tags throwable

One. Overview

Springmvc, as one of the most popular MVC frameworks in the world, continues the front-end controller mode to complete the design design.

But Springmvc's ability to get IOC and AOP, with its seamless connection to spring, is a lot more powerful.

This time, we need to say the principle of SPRINGMVC, and then find some extension points to complete the study of SPRINGMVC.

Two. Architecture of the SPRINGMVC

  

In the above figure, we see the entire SPRINGMVC running flow.

We find that the whole process is still more complicated, but when we analyze it later, the whole process is more complicated. But now we don't need to talk about it.

Let's start with a look at the entire process running.

[1] The user initiates a request to a Web browser.

[2] The request is forwarded to the front controller and into the Springmvc control flow.

[3] The first request is forwarded to the {processor Mapper}, which returns a processor execution chain, which includes the processor and the interceptor.

[4] The front-end controller forwards the processor chain to the processor adapter for true release execution, where there are many extensibility points.

[5] View rendering

All we need to do now is simply to understand the process.

Three. Front Controller

SPRINGMVC's front-end controller is a servlet, let's first look at the basic structure of this servlet.

The following is the core method of the entire SPRINGMVC Dispatcherservlet Dodispatch (). Let's analyze the process first.

protected voidDodispatch (httpservletrequest request, httpservletresponse response) throws Exception {HttpServletRequest Pro Cessedrequest=request; Replace the request with a reference to a processing request Handlerexecutionchain Mappedhandler=NULL; Defining the processor mass chain Boolean multipartrequestparsed=false; Is it related to file upload//This is asynchronous behavior, we don't care about Webasyncmanager here Asyncmanager=Webasyncutils.getasyncmanager (Request); Try{modelandview mv=NULL; return value Exception dispatchexception=NULL;//ExceptionTry{processedrequest=Checkmultipart (request);//Check if file upload is included multipartrequestparsed= (Processedrequest! =request); If it is a file upload, make the requested conversion//determine handler for the current request.Mappedhandler =gethandler (processedrequest);//request to find the processor mapper for the objectif(Mappedhandler = =NULL|| Mappedhandler.gethandler () = =NULL) {Nohandlerfound (processedrequest, response); Without the processor mapper, it means that the address of the request is not registeredreturn; }                //Determine handler adapter for the current request.
          
1. First get the processor execution chain from the processor mapper
2/Then convert the processor execution chain to the processor adapter
Handleradapter ha =Gethandleradapter (Mappedhandler.gethandler ()); //Process last-modified Header, if supported by the handler.String method =Request.getmethod (); Boolean Isget="GET". Equals (method); if(Isget | |"HEAD". Equals (method)) { LongLastModified =ha.getlastmodified (Request, Mappedhandler.gethandler ()); if(logger.isdebugenabled ()) {Logger.debug ("last-modified value for ["+ Getrequesturi (Request) +"] is:"+lastmodified); } if(NewServletwebrequest (Request, Response). Checknotmodified (lastmodified) &&isget) { return; }}//The pre-interception behavior of the processor execution chainif(!Mappedhandler.applyprehandle (processedrequest, response)) { return; }//Call a real processor method//actually invoke the handler.MV =Ha.handle (processedrequest, Response, Mappedhandler.gethandler ()); if(asyncmanager.isconcurrenthandlingstarted ()) {return; }//Convert views and other behaviors such as storing values for domain objects such as applydefaultviewname (Processedrequest, MV);
The post-processing behavior of the processor intercept chain should be mappedhandler.applyposthandle (processedrequest, response, MV); } Catch(Exception ex) {dispatchexception=ex;//If an exception occurs, the exception has a whole place to deal with it}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 =NewNestedservletexception ("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, NewNestedservletexception ("Handler processing failed", err)); } finally { if(asyncmanager.isconcurrenthandlingstarted ()) {//Instead of Posthandle and Aftercompletion if(Mappedhandler! =NULL) {mappedhandler.applyafterconcurrenthandlingstarted (processedrequest, response); } } Else { //used by a multipart request. if(multipartrequestparsed) {Cleanupmultipart (processedrequest); } } } }

From the above code, we analyzed the springmvc of the whole process of operation, single from the process is relatively simple, but the focus is

Find the extension point and the principle, the final comparison completes the study finished SPRINGMVC.

001 SPRINGMVC Overview

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.