2

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?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Robert
  • 7,394
  • 40
  • 45
  • 64
  • 1
    How dynamic? A `c:forEach`works – Kukeltje Nov 01 '17 at 23:26
  • Please create an answer – Kukeltje Nov 02 '17 at 22:08
  • I don't have any concrete example at hand so I would either need to create one or write one from the top of my head. You have one I assume, you you can (and are encouraged) to answer your own questions. Cheers – Kukeltje Nov 03 '17 at 21:58
  • Credit is not that important in many cases. Sometimes it (relatively) takes just to much time writing an answer. Time I'd rather spend doing other things. Being able to help you solve your problem is sufficient for me. – Kukeltje Nov 03 '17 at 22:02
  • just 'mention' me in the answer ;-) – Kukeltje Nov 03 '17 at 22:02

1 Answers1

2

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!

Robert
  • 7,394
  • 40
  • 45
  • 64