The page content cannot be displayed normally when the program has no errors

Is the annotation @RestController used in the controller layer?

@RestController annotation is equivalent to the combined effect of @ResponseBody + @Controller. If you just use the @RestController annotation, you cannot return jsp and html pages, because the @RestController annotation is used to return JSON data or XML data, and will not parse jsp, html and other pages. The returned content is the content in return. Such as:

@RestController
public class UserController {
    
    
    @Resource
    private UserService userService;

    //查询所有
    @RequestMapping("/list")
    public String userList(Model model){
    
    
        List<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "index";
    }

The result after running in the browser is:
!](https://img-blog.csdnimg.cn/20201124191753478.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDA2NzMzMw==,size_16,color_FFFFFF,t_70#pic_center)

Change the @RestController annotation to @Controller page to display normally:

//@RestController
@Controller
public class UserController {
    
    
    @Resource
    private UserService userService;

    //查询所有
    @RequestMapping("/list")
    public String userList(Model model){
    
    
        List<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "index";
    }

Insert picture description here

Hope it helps you

Guess you like

Origin blog.csdn.net/weixin_44067333/article/details/110091703