【问题标题】:Make Thymeleaf read object model array让 Thymeleaf 读取对象模型数组
【发布时间】:2019-08-29 10:47:29
【问题描述】:

我正在使用 Thymeleaf 来阅读这个 Spring (JPA) 模型:

@Entity
@Table(name = "category", schema = "dbo", catalog = "myapp")
public class CategoryEntity {
    private int id; //works
    ...
    private List<BookEntity> books; //doesn't work

    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

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

@OneToMany(mappedBy="category")
    public List<BookEntity> getBooks() {
        return books;
    }

    public void setLineas(List<BookEntityEntity> books) {
        this.books = books;
    }

我通过控制器将它包含在模型中,例如:

 model.addAttribute("categories", repository.findAll());

我访问的字段如下:

 <tr th:each ="category ${categories}">
         <td th:text="${category.id}"></td>
</tr>

它会显示类别 ID。

但是列表字段没有被序列化,无法通过 Thymeleaf 访问。如何将书单列为类别实体的参数?我可以选择 Thymeleaf 处理哪些参数吗? 目的是遍历上述书籍:

<tr th:each ="book ${category.books}">
             <td th:text="${book.title}"></td>
</tr>

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa thymeleaf


    【解决方案1】:

    我刚刚在列表中添加了@Valid 注释并且它起作用了。似乎需要特殊类型以避免不必要地列出大量数据。

    @Valid
     private List<BookEntity> books;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 2015-06-10
      相关资源
      最近更新 更多