初次学习Thymeleaf所遇到的问题——只返回了字符串到浏览器

以前一直用jsp和Vue。现在新项目决定使用thymeleaf模板引擎,所以花点时间学下thymeleaf。没成想一开始就给自己挖了个坑。在这里记录一下,用以警示后人。

SpringBoot并不推荐使用jsp,但是支持一些模板引擎技术:

通过读取源码可知使用thymeleaf的第一步是要在resources目录下新建templates文件夹,再在该文件夹下面新建一个html文件。如下图所示。

 

废话不多说,

我的项目结构如下:

userList.html文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <style type="text/css">
        table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
        table, th, td {border: 1px solid darkslategray;padding: 10px}
    </style>
</head>
<body>
<div style="text-align: center">
    <span style="color: darkslategray; font-size: 30px">欢迎光临!</span>
    <hr/>
    <table class="list">
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>用户名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>生日</th>
        </tr>
        <tr th:each="user : ${userList}">
            <td th:text="${user.id}">1</td>
            <td th:text="${user.name}">张三</td>
            <td th:text="${user.userName}">zhangsan</td>
            <td th:text="${user.age}">20</td>
            <td th:text="${user.sex}">男</td>
            <td th:text="${user.birthday}">1980-02-30</td>
        </tr>
    </table>
</div>
</body>
</html>

User类:

package cn.chao.user.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 * @author
 * @create 2020-02-10 22:00
 **/
@Table(name = "tb_user")
public class User {
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String userName;

    private String password;

    private String name;

    private Integer age;

    private Integer sex;

    private Date birthday;

    private Date created;

    private Date updated;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", created=" + created +
                ", updated=" + updated +
                '}';
    }
}

UserController类:

package cn.chao.user.controller;

import cn.chao.user.pojo.User;
import cn.chao.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author
 * @create 2020-02-10 19:12
 **/
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    @GetMapping("all")
    public String all(Model model) {
        // 查询用户
        List<User> userList = this.userService.queryAll();
        // 放入模型
        model.addAttribute("userList", userList);
        // 返回模板名称(就是classpath:/templates/目录下的html文件名)
        return "userList";
    }
}

启动项目,在浏览器中输入localhost/user/all

结果页面并没有加载我的静态页面,而是显示“userList”字符串。

这是为何?

经过一番苦苦查看,终于发现罪魁祸首居然是UserController类上的@RestController注解。

分析:

@RestController注解 = @Controller + @ResponseBody。加上@RestController,返回的内容是你return中的内容,如果是return "userList",页面显示的就是userList。只加上@Controller注解,返回的是return中对应的页面,比如return “userList”,页面的名称是userList。

解决方法:

把UserController类中的@RestController注解改为@Controller注解。重启项目后,如下所示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章