Setting value of a h:selectOneMenu from a for each

2019-08-20 13:21发布

This is the for each to iterate a list of products and I need to set the productGroupId in the drop down below.

 <c:forEach items="${productgroup.productList}" var="product">

 <h:selectOneMenu  value="${product.appleProdgroupId}">
<f:selectItems value="#{displayProductsBean.productGroupListDropDown}"/>
</h:selectOneMenu>

I have tried all combination but it is not working ...can anyone please help

标签: java jsf-2 jstl
1条回答
等我变得足够好
2楼-- · 2019-08-20 13:50

It's unclear what exactly you mean with "It is not working". In the code posted so far I see at least 3 possible causes:

Your first problem is that you need to use #{} syntax instead of ${} syntax in order to be able to autocreate managed beans if they do not exist in the scope yet.

<c:forEach items="#{productgroup.productList}" var="product">
    <h:selectOneMenu value="#{product.appleProdgroupId}">
        <f:selectItems value="#{displayProductsBean.productGroupListDropDown}" />
    </h:selectOneMenu>
</c:forEach>

Your second problem is potentially the <c:forEach>, but that depends on the context where this code is running. The <c:forEach> is namely a view build time tag. If the above doesn't work, you'd need <ui:repeat> instead.

<ui:repeat value="#{productgroup.productList}" var="product">
    <h:selectOneMenu value="#{product.appleProdgroupId}">
        <f:selectItems value="#{displayProductsBean.productGroupListDropDown}" />
    </h:selectOneMenu>
</ui:repeat>

If that still doesn't work, then you'd need to attach a <h:messages/> to learn about any missing conversion/validation errors.

<h:messages />

You can for example get a "Value not valid" validation error when the list behind <f:selectItems> is not properly (pre)initialized during the form submit request.

See also:

查看更多
登录 后发表回答