通过<ui:repeat>动态填充selectOneMenu中的选项

时间:2019-04-15 10:43:02

标签: user-interface jsf primefaces

我试图根据内容从GUI中的其他选择中填充一些内容在质朴的下拉菜单中。这是我要执行的操作的简化示例:

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

当我访问该页面时,它显示[1、2、3] 123和一个空的selectOneMenu。我本来希望selectOneMenu也包含选择。迭代在上述情况下非常有效,因此我不知道为什么它不显示菜单中的选项。我在做什么错了?

1 个答案:

答案 0 :(得分:2)

<ui:repeat>是一个UI组件,而<f:selectItem>是一个标记处理程序(如JSTL)。标记处理程序在视图构建期间运行,而UI组件则在视图呈现期间运行。因此,<ui:repeat>运行时,<f:selectItem>没有任何意义。

一个<c:forEach>,它也是一个标记处理程序,可以工作:

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