Mixing static and dynamic menu entries in PrimeFaces menu components

≯℡__Kan透↙ 提交于 2020-06-17 03:16:28

问题


I'd like to keep a part of my menu static in markup, and have another part dynamically generated in Java.

<p:menubar>
    <p:menuitem value="static stuff"/>
    <p:submenu label="dynamic stuff" model="#{bean.dynamicMenu}"/>
    <!-- more static stuff -->
</p:menubar>

This shows only the static items and never calls my getDynamicMenu method cause p:submenu does not take a model attribute.

I tried using ui:include within the menu structure to move the markup in an extra file and include that in different contexts, but Primefaces complained that it does not like that as child element of p:menubar and / or p:submenu.

How can I keep parts of my menu static in xhtml and parts dynamic in Java?


回答1:


While I cannot use ui:include, I can use c:forEach, as @Kukeltje pointed out in his comment:

<p:submenu label="Dynamic 0" rendered="#{list.size() eq 0">
    <p:menuitem value="Nothing here..." url="...">
</p:submenu>

<p:submenu label="Dynamic 1" rendered="#{list.size() eq 1}">
    <c:set var="node" value="#{list[0]}"/>
    <!-- more menu structure here -->
</p:submenu>

<p:submenu label="Dynamic N" rendered="#{list.size() gt 1}">
    <c:forEach items="#{list}" var="item">
        <p:submenu label="#{item}">
            <!-- more menu structure here -->
        </p:submenu>
    </c:forEach>
</p:submenu>

This allows me to present a different menu depending on my backing bean.

Thanks again, @Kukeltje!



来源:https://stackoverflow.com/questions/47064923/mixing-static-and-dynamic-menu-entries-in-primefaces-menu-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!