Spring principle of MVC

How SpringMVC works

The working principle of SpringMVC:

SpringMVC process

1. The user sends a request to the front-end controller DispatcherServlet.

2. The DispatcherServlet receives the request and calls the HandlerMapping handler mapper.

3. The processor mapper finds the specific processor (which can be searched according to the xml configuration and annotation), generates the processor object and the processor interceptor (if any) and returns it to the DispatcherServlet.

4. DispatcherServlet calls HandlerAdapter handler adapter.

5. The HandlerAdapter is adapted to call a specific processor (Controller, also called a back-end controller).

6. After the execution of the Controller is completed, it returns to ModelAndView.

7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet.

8. DispatcherServlet passes ModelAndView to ViewReslover view resolver.

9. ViewReslover returns the specific View after parsing.

10. The DispatcherServlet renders the view according to the View (that is, fills the model data into the view).

11. The DispatcherServlet responds to the user.

Component Description:

The following components typically provide implementations using frameworks:

DispatcherServlet: As a front-end controller, the center of the entire process control, controls the execution of other components, unified scheduling, reduces the coupling between components, and improves the scalability of each component.

HandlerMapping: Implement different mapping methods by extending the handler mapper, such as: configuration file method, implementation interface method, annotation method, etc. 

HandlAdapter: Support more types of handlers by extending the handler adapter.

ViewResolver: By extending the view resolver, it supports more types of view resolution, such as: jsp, freemarker, pdf, excel, etc.

Components:
1. Front-end controller DispatcherServlet (no need for engineers to develop), which is provided by the framework

: receiving requests and responding to results, which are equivalent to repeaters and central processing units. With dispatcherServlet reduces the coupling between other components.
When the user request arrives at the front controller, it is equivalent to c in the mvc mode. The dispatcherServlet is the center of the entire process control. It calls other components to process the user's request. The existence of the dispatcherServlet reduces the coupling between the components.

2. The processor mapper HandlerMapping (doesn't require engineers to develop), which is provided by the framework
: find the Handler according to the requested url
HandlerMapping is responsible for finding the Handler or the processor according to the user's request, springmvc provides different mappers to implement different mapping methods, such as : Configuration file method, implementation interface method, annotation method, etc.

3. The role of the processor adapter HandlerAdapter
: according to the specific rules (rules required by the HandlerAdapter), the Handler executes
the processor through the HandlerAdapter. This is the application of the adapter mode. By extending the adapter, more types of processors can be executed.

4. Handler (need to be developed by engineers)
Note: When writing Handler, follow the requirements of HandlerAdapter, so that the adapter can execute Handler correctly.

Handler is the back-end controller following the front-end controller of DispatcherServlet. Under the control of DispatcherServlet, Handler pairs with Specific user requests are processed.
Since Handler involves specific user business requests, engineers generally need to develop Handlers according to business requirements.

5. View resolver Viewresolver (no need for engineers to develop), which is provided by the framework
: perform view resolution, and resolve it into a real view (view) according to the logical view name. The
View Resolver is responsible for generating the View view from the processing result. Parse it into a physical view name, that is, a specific page address, then generate a View view object, and finally render the View to display the processing results to the user through the page. The springmvc framework provides many types of View views, including: jstlView, freemarkerView, pdfView, etc.
In general, model data needs to be displayed to users through page tags or page template technology, and engineers need to develop specific pages according to business needs.

6. View View (requires engineers to develop jsp...)
View is an interface, and the implementation class supports different View types (jsp, freemarker, pdf...)

The specific process steps of the core architecture are as follows:
1. First, the user sends a request -> DispatcherServlet. After the front-end controller receives the request, it does not process it by itself, but entrusts it to other parsers for processing. As a unified access point, global Process control;
2. DispatcherServlet->HandlerMapping, HandlerMapping will map the request to a HandlerExecutionChain object (including a Handler processor (page controller) object, multiple HandlerInterceptor interceptors) objects, through this strategy mode, it is easy to add New mapping strategy;
3. DispatcherServlet -> HandlerAdapter, HandlerAdapter will package the processor as an adapter, so as to support multiple types of processors, that is, the application of the adapter design pattern, so it is easy to support many types of processors;
4 , HandlerAdapter -> Invocation of the processor function processing method, HandlerAdapter will call the real processor function processing method according to the adaptation result to complete the function processing; and return a ModelAndView object (including model data, logical view name);
5. The logical view name of ModelAndView -> ViewResolver, ViewResolver will resolve the logical view name to a specific View, through this strategy mode, it is easy to replace other view technologies;
6, View -> Rendering, View will come in according to the incoming The Model data is rendered here. The Model here is actually a Map data structure, so it is easy to support other view technologies;
7. Return control to the DispatcherServlet, and the DispatcherServlet returns the response to the user, and the process ends here.

The following two components usually need to be developed:

Handler: Handler, that is, the back-end controller is represented by controller.

View: The view is the interface displayed to the user. The view usually requires a tag language to display model data.

 

Before we use SpringMVC, let's take a look at what is the MVC pattern

MVC: MVC is a design pattern

Schematic diagram of MVC:

analyze:

M-Model model (complete business logic: composed of javaBean, service+dao+entity)

V-View view (for interface display jsp, html...)

C-Controller controller (receive request -> call model -> dispatch page according to the result)

 

What is springMVC: 

  springMVC is an open source framework of MVC, springMVC=struts2+spring, springMVC is equivalent to the integration of Struts2 plus sring, but there is a doubt here, what is the relationship between springMVC and spring? There is a good explanation for this on Baidu Encyclopedia: it means that springMVC is a follow-up product of spring. In fact, spring provides the MVC module of web applications on the basis of the original, so springMVC can be simply understood as spring. It is often said that springMVC and spring are seamlessly integrated on the Internet. In fact, springMVC is a sub-module of spring, so it does not need to be integrated with spring at all.

The schematic diagram of SpringMVC:

When you see this picture, you may have a lot of doubts. Now let's take a look at the steps of this picture: (You can understand it by comparing the schematic diagram of MVC)

Step 1: The user initiates a request to the front controller (DispatcherServlet)

Step 2: The front controller requests the HandlerMappering to find the Handle: find it through xml configuration or annotations

Step 3: After finding the handler mapper (HandlerMappering) returns to the execution chain (HandlerExecutionChain) like the front controller

Step 4: The front controller (DispatcherServlet) calls the handler adapter (HandlerAdapter) to execute the handler (Handler)

Step 5: Handler adapter to execute Handler

Step 6: After the Handler is executed, return ModelAndView to the handler adapter

Step 7: The handler adapter returns the ModelAndView to the front controller

Step 8: The front controller requests the view resolver (ViewResolver) to perform view resolution

Step 9: The view resolver returns the View like the front controller

Step 10: The front controller renders the view

Step 11: The front controller responds to the user with the result

Seeing these steps, I believe that everyone feels very messy, which is normal, but here is mainly to understand several components in springMVC:

Front-end controller (DispatcherServlet): Receives requests and responds to results, equivalent to the computer's CPU.

HandlerMapping: Find the handler based on the URL

Handler: (Requires programmers to write code processing logic)

Handler Adapter: The processor will be packaged into an adapter, so that it can support multiple types of processors, analogous to notebook adapters (application of adapter mode)

View resolver (ViewResovler): perform view parsing, multi-return strings, and process them, which can be parsed into corresponding pages

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325790125&siteId=291194637