ui:repeat doesn't work with f:selectItem
Asked Answered
B

2

7

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.

Butene answered 16/11, 2011 at 13:20 Comment(0)
I
29

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:

Inellineloquent answered 16/11, 2011 at 13:29 Comment(8)
it gives me an exception, that the property name doesn't exist in UserBean !!!!!!!! why it thinks that the list is of type UserBean not User ??????Butene
Can you please tone a bit down? I gather that you're talking about a PropertyNotFoundException on usersList? Well, then that means that you don't have a public List<User> getUsersList() method in the backing bean class represented by the #{user} managed bean.Inellineloquent
Oh, you are not using JSF managed beans, but you are using Spring managed beans (you didn't tell/tag anything about this in your question, so I couldn't forsee it). Well, then use #{user.getUsersList()} if standard EL call is not possible (I don't use Spring, so I can't tell if this is indeed true). You have by the way a syntax error in your class; Public is an invalid keyword, it should be public.Inellineloquent
even when using #{user.getUsersList()} i keep getting same error.Butene
ah found it, the var name in loop was matching the ManagedBean name: @Component("user") , it was fixed by changing the var name.Butene
Yes, you're right. That would conflict. I updated the answer to reflect that as well.Inellineloquent
any ideas how to get the Selected Users in such case ?Butene
This is offtopic. Ask a new question. Hint: <ice:selectOneMenu value>.Inellineloquent
U
4

why not use f:selectItems. I think something like this would work.

<f:selectItems value="#{user.getUsersList()}" var="user" itemLabel="#{user.name}"
                                            itemValue="#{user.id}" />

EDIT also try removing the brackets from the user.getUsersList() in your code , i think its not how you call a function in jsf2

Upshaw answered 16/11, 2011 at 13:27 Comment(1)
As per your edit, this is valid in EL 2.2, but plain clumsy if it's just a simple getter.Inellineloquent

© 2022 - 2024 — McMap. All rights reserved.