我从数据库创建了一个树,并将对象作为节点。但它正在显示对象引用。我想显示对象属性值(在这种情况下为类别名称)。但我无法做到。 有人知道吗?
我的类别实体如下:
@Entity
@Table(name = "category")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
@NamedQuery(name = "Category.findByParentNullID", query = "SELECT c from Category c WHERE c.categoryParentid IS NULL"),
@NamedQuery(name = "Category.findByCategoryCategoryid", query = "SELECT c FROM Category c WHERE c.categoryCategoryid = :categoryCategoryid"),
@NamedQuery(name = "Category.findByCategoryName", query = "SELECT c FROM Category c WHERE c.categoryName = :categoryName"),
@NamedQuery(name = "Category.findByCategoryParentid", query = "SELECT c FROM Category c WHERE c.categoryParentid = :categoryParentid")})
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "category_categoryid")
private Integer categoryCategoryid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "category_name")
private String categoryName;
@Column(name = "category_parentid")
private Integer categoryParentid;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "itemCategoryCategoryid")
private Collection<Item> itemCollection;
public Category() {
}
public Category(Integer categoryCategoryid) {
this.categoryCategoryid = categoryCategoryid;
}
public Category(Integer categoryCategoryid, String categoryName) {
this.categoryCategoryid = categoryCategoryid;
this.categoryName = categoryName;
}
public Integer getCategoryCategoryid() {
return categoryCategoryid;
}
public void setCategoryCategoryid(Integer categoryCategoryid) {
this.categoryCategoryid = categoryCategoryid;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getCategoryParentid() {
return categoryParentid;
}
public void setCategoryParentid(Integer categoryParentid) {
this.categoryParentid = categoryParentid;
}
@XmlTransient
public Collection<Item> getItemCollection() {
return itemCollection;
}
public void setItemCollection(Collection<Item> itemCollection) {
this.itemCollection = itemCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (categoryCategoryid != null ? categoryCategoryid.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.categoryCategoryid == null && other.categoryCategoryid != null) || (this.categoryCategoryid != null && !this.categoryCategoryid.equals(other.categoryCategoryid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.synergytech.ims.entities.Category[ categoryCategoryid=" + categoryCategoryid + " ]";
}
}
和我的CategoryController是这样的:
@ManagedBean
@ViewScoped
public class CategoryController {
@EJB
CategoryFacade categoryFacade;
Category current;
List<Category> categorylist;
private TreeNode root;
private TreeNode actualRoot, tempRoot;
private TreeNode selectedNode;
public CategoryFacade getCategoryFacade() {
return categoryFacade;
}
public Category getCurrent() {
return current;
}
public void setCurrent(Category current) {
this.current = current;
}
public CategoryController() {
}
public List<Category> getCategorylist() {
categorylist = getCategoryFacade().findAll();
return categorylist;
}
public void setCategorylist(List<Category> categorylist) {
this.categorylist = categorylist;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
public void prepareCreate() {
if (current == null) {
current = new Category();
}
}
public List<Category> All() {
return getCategoryFacade().findAll();
}
public TreeNode makeTree() {
root = new DefaultTreeNode("Root", null);
actualRoot = new DefaultTreeNode("Root", null);
categorylist = getCategoryFacade().getByParentNullID();
for (Iterator<Category> it = categorylist.iterator(); it.hasNext();) {
Category category = it.next();
actualRoot = new DefaultTreeNode(category.getCategoryName(), root);
createTree(category.getCategoryCategoryid(), category.getCategoryName(), actualRoot);
actualRoot.setParent(root);
}
return root;
}
public void createTree(Integer Pid, String categoryName, TreeNode subRoot) {
List<Category> subRootList;
subRootList = getCategoryFacade().getByParentID(Pid);
if (!subRootList.isEmpty()) {
for (Iterator<Category> it = subRootList.iterator(); it.hasNext();) {
Category category = it.next();
TreeNode node = new DefaultTreeNode(category.getCategoryName(), subRoot);
createTree(category.getCategoryCategoryid(), category.getCategoryName(), node);
}
}
}
public void onNodeSelect(NodeSelectEvent event) {
String categoryName = event.getTreeNode().toString();
Integer pid = getCategoryFacade().getByCategoryName(categoryName);
prepareCreate();
getCurrent().setCategoryParentid(pid);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", categoryName);
FacesContext.getCurrentInstance().addMessage(null, message);
//RequestContext context = RequestContext.getCurrentInstance();
//context.execute("createDialog.show();");
}
public void onNodeUnselect(NodeUnselectEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", event.getTreeNode().toString());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
和我显示树的xhtml代码是这样的:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:form id="CategoryListForm">
<p:growl id="growl" showDetail="true" sticky="false"/>
<p:panel header="Category List">
<p:tree value="#{categoryController.makeTree()}" var="node" dynamic="true" cache="false" selectionMode="single" selection="#{categoryController.selectedNode}" id="tree">
<p:ajax event="select" update=":CategoryListForm:growl" listener="#{categoryController.onNodeSelect}"/>
<p:ajax event="unselect" update=":CategoryListForm:growl" listener="#{categoryController.onNodeUnselect}"/>
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<f:facet name="footer">
<div class="footer-section">
<p:commandButton id="createButton" icon="ui-icon-plus" value="Create" action="#{categoryController.prepareCreate()}" oncomplete="CategoryCreateDialog.show()"/>
</div>
</f:facet>
</p:panel>
</h:form>
</ui:composition>
我只想在树中的xhtml中显示属性值。但是,当我从树中选择节点时,我希望不仅要选择属性值。
答案 0 :(得分:1)
<h:outputText value="#{node}" />
这隐式调用node.toString()
,即TreeNode.toString()
,显然会返回Object.toString()
返回的内容。您需要选择要显示的节点的字段。