这个Web应用程序是用连接到MariaDB的Spring Boot制作的。我想查看每个类别中的项目,我可以看到类别属性,并且可以在 View 中看到该项目,如下所示[Item [id=1, title=test, description=11, status=Used, zipcode=1231, Price=111.0, Category=Category [id=3, type=Services]]]
这是我的 Controller
@RequestMapping(value="/item-by-category/{id}",method= RequestMethod.GET)
public String itemByCategory(@PathVariable("id") Long categoryid, Model model){
//model.addAttribute("items",irepository.findByCategory(categoryid));
model.addAttribute("categorys",crepository.findOne(categoryid));
//model.addAttribute("item",irepository.findByCategory(categoryid));
return "/item-by-category";
}
我使用 CRUD 存储库,因此我尝试使用 itemRepository 方法按类别过滤项目,但我在传递的属性类型上收到错误,即使其类型为类启动中所述的 Long 也是如此。
存储库:
public interface ItemRepository extends CrudRepository<Item, Long> {
List<Item> findByTitle(String title);
List<Item> findByCategory(Long categoryid);
}
public interface CategoryRepository extends CrudRepository<Category, Long> {
//find by cat type
List<Category> findByType(String type);
//List<Category>findByItem(String item);
//Item findByCategory(String type);
//show the items in the categories
//List<Item>items(String type);
//List<Category>findByItem(String item);
}
类(class):
@Entity
@Table(name="category")
public class Category {
//@id creates an ID column for the table
@Id
//Generates automatically a unique PK for every new entity object
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="categoryid")
private Long id;
@Column(name="type")
private String type;
// 1 Category can have * Items
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private List<Item> item;
//constructor
public Category() {}
public Category(String type) {
super();
this.type = type;
}
//getters n setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//getters n setters for relationship
public List<Item> getItems() {
return item;
}
public void setItems(List<Item> item) {
this.item = item;
}
@Override
public String toString() {
return "Category [id=" + id + ", type=" + type + "]";
}
}
@Entity
@Table(name="item")
public class Item {
//generated ID column
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="itemid")
private Long id;
@Column(name="title")
private String title;
@Column(name="description")
private String description;
@Column(name="status")
private String status;
@Column(name="zipcode",length=5)
private Integer zipcode;
@Column(name="price")
private Double price;
//create relationship or * to 1 between items to user
/*
@ManyToOne
@JsonIgnore
@JoinColumn(name = "userid")
private User user;
*/
//Many items can have 1 category * to 1
@ManyToOne
/*entity relationship will
cause endless loop (First item is serialized and it contains
category which is then serialized which contains students which
are then serialized
*/
@JsonIgnore
@JoinColumn(name = "categoryid")
private Category category;
//working ^^
//JPA constructor
public Item(){
}
public Item(Long id, String title, String description, String status, Integer zipcode, Double price,
Category category) {
super();
this.id = id;
this.title = title;
this.description = description;
this.status = status;
this.zipcode = zipcode;
this.price = price;
this.category = category;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getZipcode() {
return zipcode;
}
public void setZipcode(Integer zipcode) {
this.zipcode = zipcode;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return "Item [id=" + id + ", title=" + title + ", description=" + description + ", status=" + status
+ ", zipcode=" + zipcode + ", price=" + price + ", category=" + category + "]";
}
}
因此尝试采用该类别并使其正常运行,如下所示:
<table class="table table-striped">
<tr th:each ="category : ${categorys}">
<td th:text="${category.id}" ></td>
<td th:text="${category.items}" ></td>
//shows the size of the array
<td th:text="${category.items.size()}" ></td>
</tr>
<!-- this is what I want as the result, I would like to know how to loop with the TH attribute
<tr>
<td th:text="${category.items[0].title}"></td>
<td th:text="${category.items[0].category.type}"></td>
<td th:text="${category.items[0].description}"></td>
<td th:text="${category.items[0].status}"></td>
<td th:text="${category.items[0].zipcode}"></td>
<td th:text="${category.items[0].price}"></td>
</tr>
-->
这是我的第一个使用 Spring mvc 的 Web 应用程序,此时我有点卡住了,如果有人指出我正确的路径,我将不胜感激:D
请您参考如下方法:
要在 Thymeleaf 中实现嵌套循环,只需执行以下操作:
<th:block th:each="category : ${categorys}">
<tr th:each="item : ${category.items}">
<td th:text="${category.item.title}"></td>
<td th:text="${category.item.category.type}"></td>
<td th:text="${category.item.description}"></td>
<td th:text="${category.item.status}"></td>
<td th:text="${category.item.zipcode}"></td>
<td th:text="${category.item.price}"></td>
</tr>
</th:block>