What is Spring MVC? Explain its components, request process and annotations in detail

Author: Insist--

Personal homepage: insist--personal homepage

The author will continue to update network knowledge and python basic knowledge , looking forward to your attention

foreword

This article will explain what Spring MVC is, its advantages and disadvantages and nine major components, as well as its request process and commonly used annotations.

Table of contents

1. What is Spring MVC?

Second, the advantages and disadvantages of Spring MVC

1. Advantages

2. Disadvantages

Three, the nine components of Spring MVC

1、Handler Mapping

2、HandlerAdapter

3、HandlerExceptionResolver

4、ViewResolver

5、RequestToViewNameTranslator

6、LocaleResolver

7、ThemeResolver

8、MultipartResolver

9、FlashMapManager

Four, Spring MVC request process

5. What are the common annotations of Spring MVC?

1、@RequestMapping

2、@RequestBody

3、@ResponseBody


1. What is Spring MVC?

c49d6682fabf6b211a55e0f2dcf408cb.jpeg

Spring MVC is a Java-based lightweight web framework that implements the request-driven type of the MVC design model . It uses the idea of ​​​​the MVC architecture model to decouple the responsibility of the web layer. Based on request-driven refers to the use of request-response The purpose of the model and framework is to help us simplify development, and Spring Web MVC also simplifies our daily Web development.

Second, the advantages and disadvantages of Spring MVC

4e34b360590a4a8ea25a4a5d1c72d878.jpg

SpringMVC is a typical lightweight MVC framework that acts as a controller framework in the entire MVC architecture. Compared with the previous struts2 framework, SpringMVC runs faster and its annotation development is more efficient and flexible .

1. Advantages

①It has a powerful modular function and high code readability;

②The test data can be simply injected through POJO (Plain Ordinary Java Object, simple Java object);

③ You can use DI (DependencyInjection, dependency injection, or inversion of control) flexibly;

④ There is low coupling between different modules (the above four advantages are still low coupling in the final analysis).

2. Disadvantages

① For novices, it is more difficult to realize;

②The learning curve is relatively steep, that is, it is difficult to get started.

Three, the nine components of Spring MVC

1、Handler Mapping

Find the corresponding processor according to the Request. Because Handler (Controller) has two forms, one is class-based Handler, and the other is Method-based Handler (which is what we commonly use).

2、HandlerAdapter

Adapter to call Handler. If Handler (Controller) is regarded as a tool, then HandlerAdapter is equivalent to a worker.

3、HandlerExceptionResolver

Handling of exceptions.

4、ViewResolver

It is used to parse the String type view name and Locale into a View type view.

5、RequestToViewNameTranslator

Some Handlers (Controllers) do not set the return type after processing, such as the void method, which requires obtaining the viewName from the request.

6、LocaleResolver

Parse the Locale from the request. Locale represents an area, such as zh-cn, which displays different results for users in different areas, which is i18n (there is a specific interceptor LocaleChangeInterceptor in SpringMVC).

7、ThemeResolver

Theme analysis, this is similar to changing the theme of our mobile phone, different UI, css, etc.

8、MultipartResolver

Process upload requests and encapsulate ordinary requests into

MultipartHttpServletRequest。

9、FlashMapManager

Used to manage FlashMap, FlashMap is used to pass parameters in redirection.

Four, Spring MVC request process

In a web application, requests are essential . Each request is initiated by the user (client) and ends when the user (client) receives the response. The following figure shows each request experienced in Spring MVC process:

d58f871f9f144ce48add7f0f986bd9f4.jpg

Below I will explain each process in the diagram :

1. When the request leaves the browser (above ①), there will be information about the content requested by the user, such as the requested URL and the submitted form information.

2. Then enter the first stop of the request, namely Spring's DispatcherServlet, whose task is to send the request to the Spring MVC controller.

3. Because there will be multiple controllers in the application, DispatcherServlet will query one or more handler mappings, and the handler mapping will determine which controller the request should be sent to according to the URL carried by the request (Picture ② above).

4. After the controller is determined, DispatcherServlet will send the request to the determined controller, and then wait for the controller to process the information submitted by the user. However, in general, the controller itself hardly handles the work, but delegates the business logic to One or more service objects for processing (③ in the figure).

5. After the controller completes the business logic processing, it usually generates some model (Model) information, which needs to be returned to the user and displayed on the browser. In order to display this information more friendly, such as in html format, we need to The information is sent to a view (View), such as JSP.

6. The last thing the controller does is to package the model data and mark the name of the view used for rendering output. It will send the request back to DispatcherServlet (above ④) with the model and view name, but the controller will only return a logical name of the view instead of returning a specific specific view. This logical name will be used to find the result real view. DispatcherServlet will use the view resolver (view resolver) to match the logical view name to a specific view (5 above), such as JSP or Thymeleaf.

7. The last stop of the request is the realization of the view (⑥ above), where the view will use the model data to render the output, and this output will be passed to the user/client through the response object (⑦ above).

5. What are the common annotations of Spring MVC?

1、@RequestMapping

An annotation for processing request url mapping , which can be used on classes or methods. When used on a class, it means that all methods in the class that respond to requests use this address as the parent path.

2、@RequestBody

The annotation realizes receiving the json data of the http request and converting the json into a java object.

3、@ResponseBody

The annotation implementation converts the object returned by the conreoller method into a json object to respond to the client.

Guess you like

Origin blog.csdn.net/m0_73995538/article/details/131506230