简体   繁体   English

如何在jsp上显示带有某些对象的嵌套树

[英]How to display nested tree with some object on jsp


I want to display on my jsp something like this 我想在我的jsp上显示这样的内容

CategoryParent1 类别父母1
-----Subcategory1 -----子类别1
-----Subcategory2 -----子类别2
CategoryParent2 CategoryParent2
-----Subcategory1 -----子类别1
-----Subcategory2 -----子类别2

In which way i can solve it? 我可以用哪种方式解决呢? Does spring-mvc has some library for it? spring-mvc是否有一些库? Or i must use some external libraries? 还是我必须使用一些外部库?

I have a Category model 我有一个类别模型

@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
private Category parentCategory;
@OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL)
private List<Category> childCategories = new ArrayList<>();

public Category() {
}

public Category(String name){
    this.name = name;
}

public Category(String name, Category parentCategory) {
    this.name = name;
    this.parentCategory = parentCategory;
}

public Integer getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Category getParentCategory() {
    return parentCategory;
}

public void setParentCategory(Category parentCategory) {
    this.parentCategory = parentCategory;
}

public List<Category> getChildCategories() {
    return childCategories;
}

public void setChildCategories(List<Category> childCategories) {
    this.childCategories = childCategories;
}
}

I have done the similar task but with recursive query. 我已经完成了类似的任务,但是使用了递归查询。 Here is the link 链接在这里

If you don't want to use recursive query. 如果您不想使用递归查询。 here is what you need to do. 这是您需要做的。

  • Eagerly fetch child categories (see hibernate eager fetch| lazy load) 渴望获取子类别(请参见休眠渴望获取|延迟加载)
  • Remove the "sub query" which is inside the "main query" and remove the recursing statement of a function 删除“主查询”内部的“子查询”,并删除函数的递归语句
  • If you have successfully created relationship, you can just get its children categories by using your "getchildrencategories" method. 如果您已成功创建关系,则可以使用“ getchildrencategories”方法获取其子类别。
  • It is your choice whether you use recursive function or not. 是否使用递归函数由您选择。

And do not use List its better to use Set: 并且不要使用List最好使用Set:

@OneToMany(fetch = FetchType.EAGER, mappedBy="parentCategory", cascade=CascadeType.REMOVE, orphanRemoval=true )
private Set<Categories> childCategories = new HashSet<Categories>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM