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.

Andrea
  • 1,057
  • 1
  • 20
  • 49
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

2 Answers2

28

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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 ?????? – Mahmoud Saleh Nov 16 '11 at 14:16
  • 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 getUsersList()` method in the backing bean class represented by the `#{user}` managed bean. – BalusC Nov 16 '11 at 14:19
  • 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`. – BalusC Nov 16 '11 at 14:22
  • even when using #{user.getUsersList()} i keep getting same error. – Mahmoud Saleh Nov 16 '11 at 14:29
  • ah found it, the var name in loop was matching the ManagedBean name: @Component("user") , it was fixed by changing the var name. – Mahmoud Saleh Nov 16 '11 at 14:32
  • Yes, you're right. That would conflict. I updated the answer to reflect that as well. – BalusC Nov 16 '11 at 14:36
  • any ideas how to get the Selected Users in such case ? – Mahmoud Saleh Nov 16 '11 at 15:48
  • This is offtopic. Ask a new question. Hint: ``. – BalusC Nov 16 '11 at 15:55
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

Khizar
  • 2,288
  • 5
  • 32
  • 53
  • 3
    As per your edit, this is valid in EL 2.2, but plain clumsy if it's just a simple getter. – BalusC Nov 16 '11 at 13:47