如何显示数据库中的菜单项 [英] How to display menu items from database

查看:131
本文介绍了如何显示数据库中的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two classes representing menu items.

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

First one is Category, it represents the parent menu items.

@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,它表示子菜单项.

Second is SubCategory, it represent the child menu items.

@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网页上显示它们,例如:

Two class have many to one relationship. I want to display them on my a JSF 2.0/Facelets webpage like:

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

我该怎么做?

推荐答案

根据问题的注释,您似乎很难在视图侧显示它们,这显然是JSF.根据您的问题历史记录,您似乎在Facelets上使用JSF2作为查看技术.

As per the comments on the question, you seem to have trouble with displaying them in the view side, which is apparently JSF. As per your question history, you seem to be using JSF2 on Facelets as view technology.

您可以仅使用ui:repeat遍历类别及其子类别.假设您有一个受管理的Bean,它通过#{bean.categories}返回List<Category>,这是一个示例:

You can just use ui:repeat to iterate over the categories and their subcategories. Assuming that you've a managed bean which returns a List<Category> by #{bean.categories}, here's an example:

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

这篇关于如何显示数据库中的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆