5

I'm trying to let user select an item of a collection from a dropdown list in JSF. This is the code I'm using:

<f:view>
 <h:form id="insert">
    <h:selectOneMenu value="#{MyBean.user}">
        <f:selectItems value="#{MyBean.userList}" var="currentUser" itemValue="#{currentUser.username}" itemLabel="#{currentUser.username}"/>
     </h:selectOneMenu>
     <h:commandButton value="Insert" action="#{AuctionBean.insertItem}"/><br>
 </h:form>
</f:view>

And this is MyBean's code:

@ManagedBean
public class MyBean{
    private String user;
    private Collection<User> userList;

    @PostConstruct
    public void init() {
                this.userList = UserRepository.getInstance().findAllUsers();
    }
    ...
    public String insertItem() {
         System.out.println("The selected user is " + this.user);
         ...
         return ("successfulInsertion");
    }
...
}

And if needed my getter and setter for user:

public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }

My problem is that when it prints "The selected user is " there's not written the user.toString(), but userList.toString()! It's like the selectOneMenu it's not correctly setted, but I've searched a lot about it. Anyone can help? Thanks, AN

andreaxi
  • 921
  • 5
  • 15
  • 27

2 Answers2

3

The <f:selectItems> doesn't support Collection. You need a List or Map or Object[].

See also:


Update: it turns out that you're using JSP instead of Facelets. The new JSF 2.x tags and attributes are not available for JSP. This includes the <f:selectItems var>. Only the old JSF 1.x tags and attributes are available for JSP. Since JSF 2.0, JSP has been deprecated and succeeded by Facelets. You should be using Facelets instead.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • So I should make my userList a List (eg LinkedList) to make it work in this way? Thanks! – andreaxi Jun 10 '12 at 14:10
  • Yes. Use `ArrayList` as it's faster on retrieval by index. – BalusC Jun 10 '12 at 14:11
  • It's still not working... This is what i receive: The user is [User[g.riva, Gigi, Riva, ***, g.riva@abc.com], User[he, asd, adsdas, **, asdasd], User[mario.rossi, Mario, Rossi, ***, mario.rossi@gmail.com], User[paolo.bianchi, Paolo, Bianchi, ***, paolo.bianchi@hotmail.it]]. – andreaxi Jun 10 '12 at 14:29
  • I changed the type of userList in ArrayList and filled it in this way: this.userList = new ArrayList(UserRepository.getInstance().findAllUsers()); – andreaxi Jun 10 '12 at 14:30
  • I don't know if i'm making any sort of mistake... Thanks, AN – andreaxi Jun 10 '12 at 14:31
  • As per the comments on the other answer you seem to be still using legacy JSP while using JSF 2.0. This is not right. – BalusC Jun 10 '12 at 15:03
  • That solved the problem, I made it using JSF 2.0 and it worked. Thanks, AN – andreaxi Jun 11 '12 at 15:28
1

Add <f:ajax/> to your <h:selectOneMenu value="#{MyBean.user}">

like this

<h:selectOneMenu value="#{MyBean.user}">
    <f:ajax/>
    <f:selectItems value="#{MyBean.userList}" var="currentUser" itemValue="#{currentUser.username}" itemLabel="#{currentUser.username}"/>
</h:selectOneMenu>

this will submit your selection to the server each time you will choose a value from the dropdown...

OR

add <f:ajax execute="@form"/> to your button so it will submit the selection of your dropdown menu before the call to insertItem

<h:commandButton value="Insert" action="#{AuctionBean.insertItem}">
    <f:ajax execute="@form"/>
</h:commandButton> 
Daniel
  • 35,598
  • 9
  • 108
  • 184