【发布时间】: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