Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SpringMVC work?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the working principle of SpringMVC", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the working principle of SpringMVC" article.

How SpringMVC works

Schematic diagram of SpringMVC:

SpringMVC process

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

2. DispatcherServlet receives a request to call the HandlerMapping processor mapper.

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

4. DispatcherServlet calls the HandlerAdapter processor adapter.

5. HandlerAdapter calls the specific processor (Controller, also known as the back-end controller) after adaptation.

6. Controller returns ModelAndView when the execution is completed.

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

8. DispatcherServlet passes ModelAndView to the ViewReslover view parser.

9. The specific View is returned after ViewReslover parsing.

10. DispatcherServlet renders the view according to the View (populating the model data into the view).

11. DispatcherServlet responds to users.

Component description:

The following components typically use frameworks to provide implementations:

DispatcherServlet: as the front-end controller, the center of the whole process control, control the execution of other components, unified scheduling, reduce the coupling between components, and improve the expansibility of each component.

HandlerMapping: different mapping methods are implemented by extending the processor mapper, such as configuration file mode, implementation interface mode, annotation mode, etc.

HandlAdapter: support more types of processors by extending processor adapters.

ViewResolver: support more types of view parsing, such as jsp, freemarker, pdf, excel, etc., by extending the view parser.

Components:

1. Front-end controller DispatcherServlet (does not need to be developed by engineers), which is provided by the framework

Function: receive requests, respond to results, equivalent to transponders, central processing units. With dispatcherServlet, the coupling between other components is reduced.

When the user request arrives at the front-end controller, it is equivalent to the mvc pattern in which the cpowerhouse Servlet is the center of the whole process control. It invokes other components to deal with the user's request. The existence of dispatcherServlet reduces the coupling between components.

2. Processor mapper HandlerMapping (which does not require engineer development), provided by the framework

Function: to find the Handler based on the requested url

HandlerMapping is responsible for finding the Handler processor according to the user's request. Springmvc provides different mappers to achieve different mapping methods, such as configuration file mode, implementation interface mode, annotation mode and so on.

3. Processor Adapter HandlerAdapter

Function: execute Handler according to specific rules (rules required by HandlerAdapter)

Processors are executed through HandlerAdapter, which is an application of the adapter pattern, and more types of processors can be executed by extending adapters.

4. Processor Handler (needs 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 DispatcherServlet front-end controller. Handler processes specific user requests under the control of DispatcherServlet.

Because Handler involves specific user business requests, engineers are generally required to develop Handler according to business requirements.

5. View parser View resolver (which does not need to be developed by engineers), provided by the framework

Function: perform view parsing, parsing to a real view according to the logical view name (view)

View Resolver is responsible for generating the View view of the processing result. View Resolver first parses the logical view name into the physical view name, that is, the specific page address, and then generates the View view object. Finally, the View is rendered and the processing result is displayed to the user through the page. The springmvc framework provides many types of View views, including jstlView, freemarkerView, pdfView, and so on.

In general, the model data needs to be displayed to the user through the page through the page tag or page template technology, and the specific page needs to be developed by the engineer according to the business requirements.

6. View View (engineer is required 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 the request-- > DispatcherServlet. After receiving the request, the front-end controller does not process it by itself, but entrusts it to other parsers to handle it as a unified access point for global process control.

2. DispatcherServlet-- > HandlerMapping, HandlerMapping will map the request to a HandlerExecutionChain object (including a Handler processor (page controller) object and multiple HandlerInterceptor interceptors). Through this policy mode, it is easy to add new mapping policies.

3. DispatcherServlet-- > HandlerAdapter,HandlerAdapter will package processors as adapters to support multiple types of processors, that is, the application of adapter design patterns, so that many types of processors can be easily supported.

4. HandlerAdapter-- > the call of the processor function processing method. HandlerAdapter will call the real processor function processing method according to the adaptation result, 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 parse the logical view name into a specific View. Through this policy mode, it is easy to change other view technologies.

6. View-- > render. View will render based on the incoming Model model data. The Model here is actually a Map data structure, so it is easy to support other view technologies.

7. Return the control to DispatcherServlet, and the DispatcherServlet returns the response to the user, until the end of the process.

The following two components typically require development:

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

View: the view, which is the interface that is presented to the user, usually requires a tag language to display the model data.

Before we SpringMVC, let's take a look at what MVC mode is.

MVC:MVC is a design pattern

Schematic diagram of MVC:

Analysis:

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

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

C-Controller controller (receive request-> call model-> dispatch pages based on results)

What is springMVC:

SpringMVC is an open source framework for MVC, and springMVC=struts2+spring,springMVC is the equivalent of the integration of Struts2 and 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 application on the original basis. SpringMVC can be simply understood as a module of spring (a module like AOP,IOC). It is often said on the network that springMVC and spring are seamlessly integrated. In fact, springMVC is a sub-module of spring, so there is no need to integrate with spring at all.

Schematic diagram of SpringMVC:

You may have a lot of doubts when you see this diagram. Now let's take a look at the steps of this diagram: (can be understood by comparing the schematic diagram of MVC)

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

Step 2: the front-end controller requests the processor mapper (HandlerMappering) to find the processor (Handle): find it through xml configuration or comments

Step 3: find the post-processor mapper (HandlerMappering) like the front-end controller returns the execution chain (HandlerExecutionChain)

Step 4: the front-end controller (DispatcherServlet) calls the processor adapter (HandlerAdapter) to execute the processor (Handler)

Step 5: processor adapter to execute Handler

Step 6: Handler returns ModelAndView to the processor adapter after execution

Step 7: the processor adapter returns ModelAndView to the front controller

Step 8: the front-end controller requests the view parser (ViewResolver) to parse the view

Step 9: the view parser returns View like the front-end controller

Step 10: the front controller renders the view

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

I'm sure you feel very confused when you see these steps, which is normal, but the main purpose here is to understand several components of springMVC:

Front-end controller (DispatcherServlet): receives the request and responds to the result, which is equivalent to the CPU of the computer.

Processor Mapper (HandlerMapping): find processors based on URL

Processor (Handler): (requires programmers to write code processing logic)

Processor adapter (HandlerAdapter): the processor is packaged as an adapter so that it can support multiple types of processors, analogous to notebook adapters (application of adapter patterns)

View parser (ViewResovler): parses views. Multiple returned strings are processed and can be parsed into corresponding pages.

The above is about the content of this article on "how SpringMVC works". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report