2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
pom.xml file imports lombok dependencies
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.34</version>
- </dependency>
@Controller indicates that this is a controller
@RequestParam indicates receiving from the front end
The returned results are generally passed to the front end using Model
- package com.demo.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
- //localhost:8080/user/test?name=xx
- @GetMapping("/test")
- public String test(@RequestParam("name") String name, Model model){
-
- //1.接收前端参数
- System.out.println(name);
-
- //2.将返回的结果传递给前端
- model.addAttribute("msg",name);
-
- //3.视图跳转
- return "test"; //test.jsp
-
- }
- }
@AllArgsConstructor Constructor with parameters
@NoArgsConstructor No-argument constructor
(You need to import lombok's jar package before you can reference it)
- package com.demo.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor //有参构造器
- @NoArgsConstructor //无参构造器
- public class User {
- private String name;
- private int age;
- }
Receive the parameters passed by the front-end user, determine the name of the parameter, and if the name is in the method, you can use it directly
The parameters passed must be consistent with the parameter fields received by the object
- package com.demo.controller;
-
- import com.demo.pojo.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
-
- /*
- 1.接收前端用户传递的参数,判断参数的名字,假设名字在方法上,可以直接使用
- 2.假设传递的是一个对象User,匹配User对象中的字段名
- */
- @GetMapping("/test2")
- public String test2(User user){
- System.out.println(user);
- return "test";
- }
- }
For example, enter http:localhost:8080/user/test2?name=hh&age=18 in the address bar
(You need to set parameters according to the class you created. In the above example, the User class is used, so the variables in the User class are used.)
The output is:
Method for displaying data to the front end:
1. ModelAndView
First create a new ModelAndView()
Then encapsulate the data through addObject
Finally, setViewName sets the returned view name
2. Model
Use the Model interface in method brackets
Encapsulate data through addAttribute
Finally return to the view
3. ModelMap
Press ctrl + h to view the tree
ModelMap inherits LinkedHashMap and has all its functions
The usage is similar to Model
Comparison of the three:
1. Model has only a few methods suitable for storing data
2. ModelMap not only implements its own methods, but also inherits the methods and features of LinkedHashMap
3. ModelAndView can set the returned logical view while storing data to control the jump of the display layer