1

I'm trying to populate some dropdown menus in primefaces with content depending on some choices from other selections in the GUI. This is a simplified example of what I'm trying to do:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" >
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>
        <h:form>
            <c:set var="options" value="#{['1','2','3']}" />
            <c:set var="currentValue" value="#{3}" />
            <h:outputText value="${options}" />
            <ui:repeat var="r" value="#{options}">
                <h:outputText value="#{r}" />
            </ui:repeat>
            <c:set var="currentValue" value="#{currentValue}" />
            <p:selectOneMenu id="selectValue" 
                             value="${currentValue}" 
                             class="pFieldSet_Template_Input200 r10">
                <p:ajax event="change" />
                <ui:repeat var="r" value="#{options}">
                    <f:selectItem itemLabel="Choice #{r} (20180101)" itemValue="#{r}" />
                </ui:repeat>
            </p:selectOneMenu>
        </h:form>
    </h:body>
</html>

When I visit the page it shows [1, 2, 3]123 and an empty selectOneMenu. I would have expected the selectOneMenu to contain the choices as well. The iteration obivously works in the above case so I don't know why it doesn't show the options in the menu. What am I doing wrong?

Mattias
  • 161
  • 1
  • 13
  • 2
    Why don't you simply use ``? – Jasper de Vries Apr 15 '19 at 10:47
  • 1
    if you do not want it to do as @JasperdeVries suggests, you should use `c:foreach` instead but that is less dynamic. Read https://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense then. And the same would fail with a plain jsf select. So it is not PrimeFaces related (always check if some generic component also fails) – Kukeltje Apr 15 '19 at 11:24
  • @jasper-de-vries I wasn't aware you could do like that. Worked like a charm after a little fiddling. Thanks. – Mattias Apr 15 '19 at 11:46
  • 1
    Possible duplicate of [ui:repeat doesn't work with f:selectItem](https://stackoverflow.com/questions/8152322/uirepeat-doesnt-work-with-fselectitem) – Kukeltje Apr 15 '19 at 12:05

1 Answers1

6

The <ui:repeat> is an UI component while <f:selectItem> is a taghandler (like JSTL). Taghandlers runs during view build time before UI components which runs during view render time. So at the moment the <ui:repeat> runs, there is no means of a <f:selectItem>.

A <c:forEach>, which is also a tag handler, would work:

<p:selectOneMenu id="selectValue" 
                             value="${currentValue}" 
                             class="pFieldSet_Template_Input200 r10">
                <p:ajax event="change" />
    <c:forEach items="#{options}" var="r">
       <f:selectItem itemLabel="Choice #{r} (20180101)" itemValue="#{r}" />
    </c:forEach>
</p:selectOneMenu>
Ismail
  • 2,322
  • 1
  • 12
  • 26
  • Made it a bit clearer why it doesn't work. Thank you. – Mattias Apr 15 '19 at 11:47
  • 1
    Many (if not most or even all) of these sort of questions have a duplicate Q/A in stackoverflow. Instead of answering them, it is common to refer to the duplicate. https://stackoverflow.com/questions/8152322/uirepeat-doesnt-work-with-fselectitem – Kukeltje Apr 15 '19 at 12:05