From the title we can see a very unfamiliar term "Spring Web MVC", this term sounds very high-sounding, but what exactly is it? This is the explanation of the official document: Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework since the beginning. Its official name "Spring Web MVC" comes from the name of its source module (Spring-webmvc), but it is often referred to as SpringMVC
It doesn't matter if you don't understand, just put it in plain words:Spring Web MVC is a web framework However, to truly understand Spring MVC, we need to understand what MVC is. In fact, MVC is an idea. MVC is the abbreviation of Model View Controller. It is a software architecture design pattern in software engineering. It divides the software system into three basic parts: model, view and controller, as shown in the following figure You can understand the definitions of these parts: • View refers to the resources in the application that are specifically used to interact with the browser and display data. • Model is the main part of the application, which is used to handle the data logic in the program. • Controller can be understood as a distributor, which is used to decide which model to use to process the request sent by the view, and which view to jump back to after processing. That is, it is used to connect the view and the model
The main process is: View sends a request to Contoller, and Contoller selects which solution (solution model) can solve the request. After the selection, it sends instructions to Model to select the model to handle the problem. After the processing is completed, Model returns the processing result to Controller, and then Controller returns the result to View. This is the main framework of MVC, and it is also a way of solving problems.
For example, going to a restaurant for dinner After customers enter the store, the waiter will take their orders. After customers order, they will hand over their menus to the front office, who will give orders to the kitchen according to the menu. The kitchen will cook, and after cooking, they will tell the waiter according to the menu that this is the meal for the guests at table X. During this process The waiter is the View, responsible for receiving customers, helping them order food, and serving food to customers. The front office is the Controller, which chooses which kitchen to issue orders to based on the user's order. The kitchen is the model, fulfilling the dining needs of customers according to the requirements of the front office.
To sum up: MVC is an architectural design pattern and an idea, and Spring MVC is a specific implementation of the MVC idea. In addition, Spring MVC is also a Web framework. In summary, Spring MVC is a web framework that implements the MVC pattern. Since SpringBoot is a specific implementation of SpringMVC, the projects we create are generally SpringBoot. This also explains why some students create projects with SpringBoot. Spring Boot can add many dependencies and use these dependencies to achieve different functions. Spring Boot implements web functions by adding the Spring WebMVC framework.
For example: the kitchen can be used to cook, but what really realizes the cooking function is fire and various cooking-related ingredients and tools. The kitchen is like SpringBoot. The kitchen can be equipped with cabinets to realize the storage function, and gas stoves, etc., to realize the cooking function. Cooking is MVC. Thousands of years ago, cooking was possible with fire and ingredients. However, when implementing MVC, Spring also made some changes based on the characteristics of its own project.
Based on the example of ordering food above, the main change is that we can go directly into the restaurant and order food at the front desk, instead of indirectly calling the waiter to come and take the order.
How to learn Spring MVC?
After a brief understanding of what Spring MVC is, we can have a preliminary understanding of how to learn Spring MVC. When we study Spring MVC, the focus is to learn how to interact with the user program through the browser. It is mainly divided into the following three aspects:
Establish a connection: connect the user (browser) and the Java program, that is, access an address to call our Spring program.
Request: When users make a request, they will bring some parameters. You need to find a way to get the parameters in the program, so the request is mainly used to get the parameters.
Response: After executing the business logic, the result of the program execution is returned to the user, which is the response.
Common Spring Web MVC Annotations
The first thing we learn is connection: Spring MVC annotations are an important part of the connection. Therefore, the order of my blog framework is closely related. The big topics can distinguish their main content, but the subjects are still closely related. Hey, there was a moment when I felt that I was really good. Okay, let's get to the point, no more jokes.
1. Use @RequestMapping in Spring MVC to implement URL routing mapping, which is the role of the browser connection program. Let's first look at how to write the code After we run it, enter the URL http://127.0.0.1:8080/hello in the browser You can see the effect. As for why this URL works, you don’t need to understand it now, but you will definitely understand it as you learn more. Effects such as
Note: We can see that I created the file in the src/main/java/com.example.j20240711 directory. A habit is that we write normal business code in src and test code in text. Because I use the professional version of IDEA2022.3.3, some pages may be different from yours, but the general framework is the same. In fact, we can also use the standard version to write Spring, but it is troublesome to install plug-ins. I chose the professional version to save trouble. If you also want to use the professional version, you can search for tutorials on CSDN. If you really can't figure it out, you can go to Taobao to buy one at a low price. It's very cheap, so you can also use the professional version. @RequestMapping is one of the most commonly used annotations in Spring Web MVC applications. It is used to register the route mapping of the interface. It means that when the service receives a request, the request with the path /sayHi will call the code of the sayHi method. Route mapping: When a user accesses a URL (which can be understood as a website address), the process of mapping the user's request to a method of a class in the program is called route mapping. Careful students will find that the above annotations have removed @RequestMapping and @RestController. What is the use of this annotation? We can try to remove it first.
You can see that 404 appears and the page cannot be found.
This is where @RestController comes in. In a project, there will be many classes, and each class may have many methods. How does the Spring program know which method to execute? Spring will scan all classes. If a class is annotated with @RestController, Spring will look at the methods in this class. Have you added the @RequestMapping annotation? Of course, its function is not limited to this. Let's use it first and then explain it in detail later.
@RequestMapping @RequestMapping can modify both classes and methods. When modifying classes and methods, the access address is the class path + method path. @RequestMapping identifies a class: sets the initial information of the request path for the mapping request @RequestMapping identifies a method: sets the specific information of the mapping request path
Notice: The URL path of @RequestMapping can be prefixed with / or without a slash. When the Spring program starts, it will make a judgment. If there is no / in front, Spring will concatenate the previous /. Normally, the URL path we add / @RequestMapping can also be a multi-layer path. When finally accessed, it is still the class path + method path, such as: The above mainly introduces two annotations. In fact, @RequestMapping has not been explained yet. I will write another blog separately for the next part of the annotations. Let me ask a question here: What if we want to know whether this annotation supports get or post? At this time, we need to write some front-end code to send data and verify whether it supports get or post. However, for a pure back-end person, we don’t understand how to write the front-end code. At this time, the role of the Postman software is reflected. The next issue will talk about its usage. Preview of the next issue: Explain the main usage of Postman and the second focus of learning Spring MVC: requests.