From the configuration of the servlet in Web. XML, the request is forwarded according to the servlet intercept url-parttern. Spring MVC Workflow Flowchart
Figure A
Figure II
Spring Workflow Description1. The user sends the request to the server, the request is captured by the spring front-end control Servelt dispatcherservlet; 2. Dispatcherservlet parses the request URL to get the request Resource Identifier (URI). Then, according to the URI, call handlermapping to obtain all related objects of the handler configuration (including handler objects and the interceptor corresponding to the handler object), and finally return as Handlerexecutionchain objects; 3. Dispatcherservlet according to the obtained handler, choose a suitable handleradapter. (
Notes: If the handleradapter is successfully obtained, the Prehandler (...) of the interceptor will start executing at this time. method) 4. Extract the model data from the request, populate the handler entry, and start executing the handler (Controller). In the process of populating the handler, depending on your configuration, spring will do some extra work for you: httpmessageconveter: Request messages such as JSON, Data such as XML) into an object that converts the object to the specified response information Data Transformation: Data conversion for a request message. such as string conversion to Integer, double data Radical: Data formatting of the request message. such as converting strings to formatted numbers or formatted dates data validation: Verifying the validity of the data (length, format, etc.), validating the results stored in Bindingresult or error 5. When handler executes, returns a Modelandview object; 6 to dispatcherservlet . Based on the returned Modelandview, select a suitable viewresolver (must be viewresolver already registered in the spring container) to return to dispatcherservlet ; 7. viewresolver combines model and view to render the view 8. Returns the rendered result to the client. spring Workflow Description Why does spring use only one servlet (Dispatcherservlet) to handle all requests? Detailed view of the Java EE design mode-front-end control mode Why Spring uses handlermapping and handleradapter to handle Handler? conforms to the single-object-oriented principle of responsibility, code architecture is clear, easy to maintain, and most importantly, code reusability is high. such as handleradapter may be used to handle a variety of handler.
1. When Dispatcherservlet receives the request, he first finds the appropriate handler to process the request. Dispatcherservlet maps each request to a handler through one or more handler mappings. The handler mapping is configured in the context of the Web application and is the bean that implements the Handlermapping interface. It is responsible for returning an appropriate handler (that is, controller) for the request. Handler mappings typically map requests to handlers (controllers) based on the URL of the request.
2. Once the Dispatcherservlet chooses the appropriate controller, it calls the controller to process the request.
3. After the controller finishes processing the request, the model and view name (sometimes the View object) is returned to Dispatcherservlet. The model contains the properties that the controller will pass to the view for display. If the name of the view is returned, it is parsed into the view object and then rendered. The basic class for binding models and views is Modelandview
4. When Dispatcherservlet receives the model and view name, it resolves the logical view name into the View object and renders it. Dispatcherservlet parses a view from one or more view parsers. The view resolver is configured in the context of the Web application and is the bean that implements the Viewresolver interface. Its task is to return the attempted object based on the logical view name.
5. Once dispatcherservlet the view name resolution as an attempted object, it renders the view object and passes the model returned by the controller. The task of the view is to present the model properties to the user.
How does Dispatcherservlet map to the controller after receiving the request?
In spring MVC, a Web request is mapped to a controller by one or more handler mapping beans declared in the context of the Web application (the bean that absorbs the handlermapping interface). Spring MVC provides several handlermapping implementations:
1. beannameurlhandlermapping (by default), he maps requests to handlers based on the URL pattern specified in the controller bean name.
eg. <bean name= "/welcome.htm" class= "Com.kevin.controller.WelcomeController" >...</bean>
When you visit the URL of http://******/welcome.htm, dispatcherservlet through the beannameurlhandlermapping mapping to find the Welcomecontroller.
2. Controllerclassnamehandlermapping, which is a map request by controller class name.
3. Simpleurlhandlermapping, map the request with a custom mapping definition.
Let's take a look at the controller.
The Controller interface is the basic interface for all the director classes in Spring MVC. By implementing this interface, you can create your own controller. In the HandleRequest () method, you can handle Web requests arbitrarily.
Abstractcontroller: If you want the controller to have some basic features, such as filtering supported HTTP methods (Get,post and head), and generating Cache-control headers in the HTTP response, You can let it extend the Abstractcontroller class.
Parameterizableviewcontroller: Used to create a controller with a parameterized view.
Simpleformcontroller: It supports the concept of a Command object (commandName) and can bind the value of a form field to an attribute of the same name on the command object.
Abstractwizardformcontroller: A basic task is defined for the processing of a wizard form. The wizard form has multiple single pages, so you must define multiple page views for the wizard form Controller. The controller can then manage the form's status across all of these form pages. The wizard form will have multiple actions, unlike Simpleformcontroller with only a single commit action. The Abstractwizardformcontroller determines the user's action based on the special request parameters, usually by specifying the action with the name of the Submit button.
_finished: Complete the wizard form.
_cancel: Cancels the wizard form.
_TARGETX: Enter the target page, where x is the index of the page starting at 0.
Multiactioncontroller: Allows you to group multiple related actions into a single controller.
Common Types of Views:
The analysis of several common viewresolver:
1. internalresourceviewresolver: Resolves the view according to the URL. Each view name is mapped to a URL by adding the prefix and suffix methods.
2. Xmlviewresolver: Parses the view from the XML configuration file. Declare the view as a spring bean and parse it by their bean name.
3. Resourcebundleviewresolver: Parses the view from the ResourceBundle.
4. To parse the view with multiple view parsers, it is important to note that you need to configure the resolution priority for the view parser that you configure. <property name= "order" value= "0"/> value is lower, the higher the priority.
Spring MVC Request Processing Flow