带有PrimeFaces的Java服务器面树
我从数据库中创建了一棵树,并将对象作为节点。但它正在显示对象引用。我想显示对象属性值(本例中为类别名称)。但是我做不到。 有人知道吗 我的类别实体如下所示:带有PrimeFaces的Java服务器面树,java,jsf,primefaces,tree,Java,Jsf,Primefaces,Tree,我从数据库中创建了一棵树,并将对象作为节点。但它正在显示对象引用。我想显示对象属性值(本例中为类别名称)。但是我做不到。 有人知道吗 我的类别实体如下所示: @Entity @Table(name = "category") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"), @NamedQuery(name = "C
@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 + " ]";
}
@实体
@表(name=“category”)
@XmlRootElement
@命名查询({
@NamedQuery(name=“Category.findAll”,query=“从类别c中选择c”),
@NamedQuery(name=“Category.findByParentNullID”,query=“从c类中选择c,其中c.categoryParentid为NULL”),
@NamedQuery(name=“Category.findByCategoryCategoryid”,query=“从c类中选择c,其中c.categoryCategoryid=:categoryCategoryid”),
@NamedQuery(name=“Category.findByCategoryName”,query=“从c类中选择c,其中c.categoryName=:categoryName”),
@NamedQuery(name=“Category.findByCategoryParentid”,query=“从c类中选择c,其中c.categoryParentid=:categoryParentid”))
公共类类别实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“category\u categoryid”)
私有整数categoryCategoryid;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“category\u name”)
私有字符串categoryName;
@列(name=“category\u parentid”)
私有整数类别parentid;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“itemCategoryCategoryid”)
私人收藏项目收藏;
公共类别(){
}
公共类别(整数类别categoryCategoryid){
this.categoryCategoryid=categoryCategoryid;
}
公共类别(整数categoryCategoryid、字符串categoryName){
this.categoryCategoryid=categoryCategoryid;
this.categoryName=categoryName;
}
公共整数getCategoryCategoryid(){
返回categoryCategoryid;
}
public void setCategoryCategoryid(整型categoryCategoryid){
this.categoryCategoryid=categoryCategoryid;
}
公共字符串getCategoryName(){
返回类别名称;
}
公共无效setCategoryName(字符串categoryName){
this.categoryName=categoryName;
}
公共整数getCategoryParentid(){
返回类别parentid;
}
public void setCategoryParentid(整型categoryParentid){
this.categoryParentid=categoryParentid;
}
@XmlTransient
公共集合getItemCollection(){
回收物品;
}
公共void setItemCollection(集合itemCollection){
this.itemCollection=itemCollection;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(categoryCategoryid!=null?categoryCategoryid.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果未设置id字段,此方法将不起作用
如果(!(类别的对象实例)){
返回false;
}
类别其他=(类别)对象;
如果((this.categoryCategoryid==null&&other.categoryCategoryid!=null)| |(this.categoryCategoryid!=null&&!this.categoryCategoryid.equals(other.categoryCategoryid))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回“com.synergytech.ims.entities.Category[categoryCategoryid=“+categoryCategoryid+””;
}
}
我的分类控制器是这样的:
@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);
}
}
<?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>
@ManagedBean
@视域
公共类类别控制器{
@EJB
分类法分类法;
类别电流;
列表分类列表;
独活根;
私有树节点实际根、临时根;
私有树节点选择节点;
公共类别facade getCategoryFacade(){
返回分类法;
}
公共类别getCurrent(){
回流;
}
公共无效设置当前(类别当前){
这个电流=电流;
}
公共类别控制器(){
}
公共列表getCategorylist(){
categorylist=getCategoryFacade().findAll();
返回类别列表;
}
public void setCategorylist(列表类别列表){
this.categorylist=categorylist;
}
公共树节点getRoot(){
返回根;
}
public void setRoot(TreeNode root){
this.root=根;
}
公共树节点getSelectedNode(){
返回selectedNode;
}
公共无效集合selectedNode(TreeNode selectedNode){
this.selectedNode=selectedNode;
}
公共无效准备创建(){
如果(当前==null){
当前=新类别();
}
}
公共列表全部(){
返回getCategoryFacade().findAll();
}
公共树节点makeTree(){
root=新的DefaultTreeNode(“root”,null);
actualRoot=新的DefaultTreeNode(“根”,null);
categorylist=getCategoryFacade().getByParentNullID();
for(Iterator it=categorylist.Iterator();it.hasNext();){
Category=it.next();
actualRoot=新的DefaultTreeNode(category.getCategoryName(),root);
createTree(category.getCategoryCategoryid(),category.getCategoryName(),actualRoot);
setParent(根);
}
返回根;
}
public void createTree(整数Pid、字符串categoryName、树节点子节点){
列表子目录;
SubrotList=getCategoryFacade().getByParentID(Pid);
如果(!subrotList.isEmpty()){
for(Iterator it=subrotlist.Iterator();it.hasNext();){
Category=it.next();
TreeNode=新的DefaultTreeNode(category.getCategoryName(),subRoot);
createTree(category.getCategoryCategoryid(),category.getCategoryName(),节点);
}
}
}
nodeselect上的公共无效(NodeSelectEvent事件){
String categoryName=event.GetTreNode().toString();
整数pid=getCategoryFacade().getByCategoryName(categoryName);
普雷帕
<h:outputText value="#{node}" />