jsf-2faceletsicefacesicefaces-1.8

ui:repeat doesn't work with f:selectItem


i am using icefaces select on menu to select a user from list of users and i want to repeat the selectItem for each user here's what i tried:

<ice:selectOneMenu id="users">
    <ui:repeat value="#{user.getUserList()}" var="user">
        <f:selectItem itemLabel="#{user.name}" itemValue="#{user.id}"/>
    </ui:repeat>               
</ice:selectOneMenu> 

UserBean:

@Component("user")
@Scope("view")
Public class UserBean{

Public List<User> getUserList() throws Exception {
        return userService.getAllUsers();
    }

}

NOTE: UserBean doesn't contains the properties id,name they exist in User entity. please advise, thanks.


Solution

  • 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, but much better is to use <f:selectItems> instead. Since JSF 2.0 it can take a collection and support the var attribute as well:

    <ice:selectOneMenu id="users">
        <f:selectItems value="#{user.usersList}" var="userItem" 
            itemLabel="#{userItem.name}" itemValue="#{userItem.id}" />
    </ice:selectOneMenu>
    

    Note that the var attribute should not clash with an existing bean in the scope.

    See also: