如何从数据库中显示菜单项

时间:2010-10-21 09:15:20

标签: java jsf jsf-2 facelets

我有两个代表菜单项的类。

第一个是Category,它代表父菜单项。

@Entity
@Table(name = "category")
@NamedQueries({
    @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
    @NamedQuery(name = "Category.findByCateId", query = "SELECT c FROM Category c WHERE c.cateId = :cateId"),
    @NamedQuery(name = "Category.findByCateName", query = "SELECT c FROM Category c WHERE c.cateName = :cateName")})
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "cate_id")
    private Integer cateId;
    @Basic(optional = false)
    @Column(name = "cate_name")
    private String cateName;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
    private List<SubCat> subCatList;

    public Category() {
    }

    public Category(Integer cateId) {
        this.cateId = cateId;
    }

    public Category(Integer cateId, String cateName) {
        this.cateId = cateId;
        this.cateName = cateName;
    }

    public Integer getCateId() {
        return cateId;
    }

    public void setCateId(Integer cateId) {
        this.cateId = cateId;
    }

    public String getCateName() {
        return cateName;
    }

    public void setCateName(String cateName) {
        this.cateName = cateName;
    }

    public List<SubCat> getSubCatList() {
        return subCatList;
    }

    public void setSubCatList(List<SubCat> subCatList) {
        this.subCatList = subCatList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (cateId != null ? cateId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.cateId == null && other.cateId != null) || (this.cateId != null && !this.cateId.equals(other.cateId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entity.Category[cateId=" + cateId + "]";
    }

}

第二个是SubCategory,它代表子菜单项。

@Entity
@Table(name = "sub_cat")
@NamedQueries({
    @NamedQuery(name = "SubCat.findAll", query = "SELECT s FROM SubCat s"),
    @NamedQuery(name = "SubCat.findBySubcatid", query = "SELECT s FROM SubCat s WHERE s.subcatid = :subcatid"),
    @NamedQuery(name = "SubCat.findBySubcatName", query = "SELECT s FROM SubCat s WHERE s.subcatName = :subcatName")})
public class SubCat implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "subcatid")
    private Integer subcatid;
    @Basic(optional = false)
    @Column(name = "subcat_name")
    private String subcatName;
    @JoinColumn(name = "cat_parent", referencedColumnName = "cate_id")
    @ManyToOne(optional = false)
    private Category category;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "subCat")
    private List<Items> itemList;

    public SubCat() {
    }

    public SubCat(Integer subcatid) {
        this.subcatid = subcatid;
    }

    public SubCat(Integer subcatid, String subcatName) {
        this.subcatid = subcatid;
        this.subcatName = subcatName;
    }

    public Integer getSubcatid() {
        return subcatid;
    }

    public void setSubcatid(Integer subcatid) {
        this.subcatid = subcatid;
    }

    public String getSubcatName() {
        return subcatName;
    }

    public void setSubcatName(String subcatName) {
        this.subcatName = subcatName;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Items> getItemList() {
        return itemList;
    }

    public void setItemList(List<Items> itemList) {
        this.itemList = itemList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (subcatid != null ? subcatid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SubCat)) {
            return false;
        }
        SubCat other = (SubCat) object;
        if ((this.subcatid == null && other.subcatid != null) || (this.subcatid != null && !this.subcatid.equals(other.subcatid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entity.SubCat[subcatid=" + subcatid + "]";
    }

}

两个班级有多对一的关系。我想在我的JSF 2.0 / Facelets网页上显示它们,如:

Category 1
    |
    ---->  Sub_Category 1 
    |
    ---->  Sub_Category 2
Category 2
    |
    ---->  Sub_Category 3
    |
    ---->  Sub_Category 4
    . . . 

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您需要一个包含3列的表格:

id int unsigned not null - 您的菜单项的ID name <whatever data type you need> not null - 要显示的菜单项的名称
parent_id int unsigned default NULL - 父菜单项的ID(如果存在)

如果parent_idNULL,那么它就是一个类别。 如果parent_idNOT NULL,则它是子类别。 您可以使用它来创建所需数量的菜单级别。

答案 1 :(得分:0)

根据对问题的评论,你似乎很难在视图方面显示它们,这显然是JSF。根据你的问题历史,你似乎在Facelets上使用JSF2作为视图技术。

您可以使用ui:repeat来迭代类别及其子类别。假设你有一个托管bean,它返回List<Category> #{bean.categories},这是一个例子:

<ul>
    <ui:repeat value="#{bean.categories}" var="category">
        <li>#{category.cateName}
            <h:panelGroup rendered="#{not empty category.subCatList}">
                <ul>
                    <ui:repeat value="#{category.subCatList}" var="subCat">
                        <li>#{subCat.subcatName}</li>
                    </ui:repeat>
                </ul>
            </h:panelGroup>
        </li>
    </ui:repeat>
</ul>