问题
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