Java 如何显示数据库中的菜单项
我有两个代表菜单项的类Java 如何显示数据库中的菜单项,java,jsf,jsf-2,facelets,Java,Jsf,Jsf 2,Facelets,我有两个代表菜单项的类 @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")
@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 + "]";
}
}
@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 + "]";
}
}
第一个是类别,它表示父菜单项
@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 + "]";
}
}
@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
. . .
我如何才能做到这一点?您需要一个包含3列的表: id int unsigned not null-菜单项的id name not null-要显示的菜单项的名称 parent_id int unsigned default NULL-父菜单项的id(如果存在) 如果parent_id为NULL,则它是一个类别。 如果parent_id不为NULL,则它是一个子类别。
您可以使用它来创建所需的任意多个菜单级别。根据对问题的评论,在视图端显示它们似乎有问题,这显然是JSF。根据您的问题历史,您似乎在Facelets上使用JSF2作为视图技术 您可以使用ui:repeat来迭代类别及其子类别。假设您有一个托管bean,它通过{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>
你好,Narf我知道,但我不知道如何用它创建动态菜单,我创建的方法数据类型是List and get all Category,一个方法get all Sub_Category List和一个方法get all Item List,我知道我必须将类别id传递给SubCat以获取SubCat列表,并将SubCat id传递给items以获取项目列表。但数据类型返回是一个列表,我怎样才能将它传递到每个项和子项的参数中呢?嗯,我没有Java方面的经验,所以我不能帮你们。我只是回答,因为你用数据库设计来标记你的问题。这是一个非常开放的问题。首先,你的前端技术是什么?我需要帮助!!请EJB3不是前端技术@Joeri知道您正在询问如何在应用程序中显示菜单。但实际上,您似乎很难从数据库中查询带有右菜单项的列表。不过,你的问题还是太模糊了。你到底面临什么问题?会发生什么?什么事不会发生?顺便说一下,您的数据模型有点过于复杂。正如Narf在回答中所建议的那样,一个包含引用PK的parent_id列的表就足够了。谢谢!我的问题是我不知道如何获取包含子目录的类别列表,我可以选择类别列表和子目录列表,我想将类别id传递到子目录,但它们是列表数据类型,我如何将参数从列表类别传递到子目录?@MYE:Category.getSubCatList没有返回所需的项目?