胸腺弹簧属性嵌套环



此WebApp是由连接到MariaDB的Spring Boot制作的。我想查看每个类别中的项目,我可以看到类别属性,并且可以在视图中看到该项目为 [Item [id=1, title=test, description=11, status=Used, zipcode=1231, price=111.0, category=Category [id=3, type=Services]]]

我的控制器

    @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";
}

im使用CRUD存储库,因此我尝试使用ItemRepository方法按类别过滤这些项目,但是即使该类型的类型长期很长,我也会在类型的属性类型上遇到错误。

存储库:

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);
}   

类:

    @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

要在胸腺中实现嵌套环,只需执行以下操作:

<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>  

我能够在没有嵌套环的情况下达到要求。通过将参数类型更改为类别,这是实体类中的属性类型。

存储库:

List<Item> findByCategory(Category categoryid); 

控制器:

    @RequestMapping(value="/item-by-category/{id}",method= RequestMethod.GET)
public String itemByCategory(@PathVariable("id") Category categoryid, Model model){
    model.addAttribute("items",irepository.findByCategory(categoryid));
    return "/itemlist";     
}

最新更新