thymeleaf spring th属性嵌套循环

时间:2017-11-22 10:05:54

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

这个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";

}

我正在使用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);
}   

班级:

    @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的第一个网络应用程序,此时我有点陷入困境,如果有人指出我走向正确的道路,我会很感激,谢谢你:D

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我能够在没有嵌套循环的情况下实现结果。通过将参数类型更改为Category,它是实体类中的属性类型。

存储库:

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