1

我需要一些示例来从数据库构建 TreeNode,以构建需要 TreeNode 的 PrimeFaces 菜单栏。这是我的菜单视图代码。

我需要使用数据库从我的 ManagedBean 构建一个 TreeNode,我的菜单使用这个 TreeNode 来绘制子菜单和菜单项。

我需要一种方法,允许我从 ManagedBean 处理来自数据库的查询并创建这些 TreeNode 的结构以及有关带有菜单的表结构的一些想法,我只希望存储菜单的标题。请帮忙 :(

    <p:menubar style="width: auto;" autoDisplay="false">
    <c:forEach items="#{treeBean.root.children}" var="nodos">
        <p:submenu label="#{nodos.getData()[0].toString()}" icon="#{nodos.getData()[2].toString()}">
            <c:forEach items="#{nodos.children}" var="hojas">
                <c:if test="#{hojas.isLeaf()==false}">
                    <p:submenu label="#{hojas.getData()[0].toString()}" icon="#{hojas.getData()[2].toString()}">
                        <c:forEach items="#{hojas.children}" var="menus">
                            <c:when test="#{menus.leaf}">
                                <c:if test="#{menus.leaf==false}">
                                    <p:submenu label="#{menus.getData().toString()}">
                                        <c:forEach items="#{menus.children}" var="menus2">
                                            <p:menuitem value="#{menus2.getData()[0].toString()}" action="#{menus2.getData()[1].toString()}"
                                            ajax="false" icon="#{menus2.getData()[2].toString()}" />
                                        </c:forEach>
                                    </p:submenu>
                                </c:if>
                                <c:if test="#{menus.leaf}">
                                    <p:menuitem value="#{menus.getData()[0].toString()}" action="#{menus.getData()[1].toString()}"
                                    ajax="false" icon="#{menus.getData()[2].toString()}" />
                                </c:if>
                            </c:when>
                        </c:forEach>
                    </p:submenu>
                </c:if>
                <c:if test="#{hojas.isLeaf()==true}">
                    <p:menuitem value="#{hojas.getData()[0].toString()}" action="#{hojas.getData()[1].toString()}"
                    ajax="false" icon="#{hojas.getData()[2].toString()}" />
                </c:if>
            </c:forEach>
            <c:if test="#{nodos.getData()[0].toString()=='Favoritos'}">
                <p:menuitem value="Agregar Favorito" action="verPrueba" ajax="false" />
            </c:if>
        </p:submenu>
    </c:forEach>
    <f:facet name="options">
        <p:commandButton value="Cerrar Sesión" icon="ui-icon-cerrarSesion" action="verPrueba"
        />
    </f:facet>
</p:menubar>
4

1 回答 1

0

我在这里找到了答案,即从数据库构建 TreeNode 并构建菜单栏的递归方法的代码:

public void recursivo(List<Menustab> liste, int id, TreeNode node) {
    subList2 = new ArrayList<Menustab>();
    subList2 = subMenus(id);
    for (Menustab k : subList2) {
        TreeNode childNode = new DefaultTreeNode(k.getTitulo(), node);
        recursivo(subList2, k.getId(), childNode);

    }

}

public static List<Menustab> subMenus(int i) {
    listaHijos = new ArrayList<Menustab>();
    for (Menustab k : getListaMenus()) {
        if (k.getPadre() == i) {
            listaHijos.add(k);
        }
    }
    return listaHijos;
}

我对 menuBar 的看法:

<h:form >


        <div id="header" align="left">

            <p:menubar  autoDisplay="false"> 
                <c:forEach items="#{constructorMenuMB.root.children}" var="nodos" >

                    <p:submenu   label="#{nodos.getData().toString()}"   >

                        <c:forEach items="#{nodos.children}" var="hojas">

                            <c:if test="#{hojas.isLeaf()==false}">


                                <p:submenu  label="#{hojas.getData().toString()}"  >




                                    <c:forEach items="#{hojas.children}" var="menus">



                                        <c:when test="#{menus.leaf}">
                                            <c:if test="#{menus.leaf==false}">
                                                <p:submenu  label="#{menus.getData().toString()}" >

                                                    <c:forEach items="#{menus.children}" var="menus2">
                                                        <p:menuitem  value="#{menus2.getData().toString()}"  ajax="false" />
                                                    </c:forEach>
                                                </p:submenu>
                                            </c:if>
                                            <c:if test="#{menus.leaf}">
                                                <p:menuitem value="#{menus.getData().toString()}"  ajax="false" />
                                            </c:if>
                                        </c:when>

                                    </c:forEach>



                                </p:submenu>

                            </c:if>

                            <c:if test="#{hojas.isLeaf()==true}">

                                <p:menuitem value="#{hojas.getData().toString()}"  ajax="false" />


                            </c:if>


                        </c:forEach>


                        <c:if test="#{nodos.getData().toString()=='Favoritos'}">

                            <p:menuitem value="Agregar Favorito" action="verPrueba"  ajax="false"/>
                        </c:if>



                    </p:submenu>

                </c:forEach>


            </p:menubar>



        </div>



    </h:form> 

现在我的完整bean:

public class ConstructorMenuMB {

private TreeNode root = new DefaultTreeNode("Root", null);
private static List<Menustab> listaMenus;
private static List<Menustab> listaHijos;
private List<Menustab> subList2;

@EJB
public MenuSB sb;

/**
 * Creates una nueva instancia de ConstructorMenuMB
 */
public ConstructorMenuMB() {
}

@PostConstruct
public void construirMenu() {
    listaMenus = new ArrayList<Menustab>();
    listaMenus = sb.consultarArbolMenus();
    recursivo(listaMenus, 1, root);
}

/**
 * Método recursivo para construir un árbol(TreeNode) desde una lista de arreglos de
 * cadenas obtenida desde la base de datos.
 *
 * @param ArrayList<String[]> lista de menus.
 * @return No devuelve ningun valor.
 * @throws No dispara ninguna excepcion.
 */
public void recursivo(List<Menustab> liste, int id, TreeNode node) {
    subList2 = new ArrayList<Menustab>();
    subList2 = subMenus(id);
    for (Menustab k : subList2) {
        TreeNode childNode = new DefaultTreeNode(k.getTitulo(), node);
        recursivo(subList2, k.getId(), childNode);

    }

}

public static List<Menustab> subMenus(int i) {
    listaHijos = new ArrayList<Menustab>();
    for (Menustab k : getListaMenus()) {
        if (k.getPadre() == i) {
            listaHijos.add(k);
        }
    }
    return listaHijos;
}

public static List<Menustab> getListaMenus() {
    return listaMenus;
}


public TreeNode getRoot() {
    return root;
}

}

和我的会话 bean,其中使用实体管理器通过菜单咨询我的表:

public class MenuSB {
@PersistenceContext(unitName = "MenuGeneralPU")
private EntityManager em;


 public List<Menustab> consultarArbolMenus(){

    return em.createNamedQuery("Menustab.findAll").getResultList();

}

}

我的菜单实体:

public class Menustab implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "titulo")
private String titulo;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "url")
private String url;
@Basic(optional = false)
@NotNull
@Column(name = "padre")
private int padre;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "icono")
private String icono;


public Menustab() {
}

public Menustab(Integer id) {
    this.id = id;
}

public Menustab(Integer id, String titulo, String url, int padre, String icono) {
    this.id = id;
    this.titulo = titulo;
    this.url = url;
    this.padre = padre;
    this.icono = icono;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public int getPadre() {
    return padre;
}

public void setPadre(int padre) {
    this.padre = padre;
}

public String getIcono() {
    return icono;
}

public void setIcono(String icono) {
    this.icono = icono;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.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 Menustab)) {
        return false;
    }
    Menustab other = (Menustab) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Entidad.Menustab[ id=" + id + " ]";
}

}

最后是我在数据库中的表的结构:

身份证 | 标题 | 网址 | 教士 | 图标

我在我的算法中使用基本的,一个菜单的 id 'ID' 和它的模式 'PADRE' 来构建一个 TreeNode 和标题 'TITULO' 的菜单文本,我正在努力绘制图标。

于 2013-04-03T16:49:08.610 回答