4

Iam new to JSF technology, currently in our project we are using JSF 2.0 with spring and hibernate integration.I have one doubt regarding h:selectOneMenu and f:selectItems. From the Database i'm getting a list of UserBeans.I'm using like

<h:selectOneMenu id="users" value="#{MyBean.user}">
<f:selectItems value="#{MyBean.userList}" var="user" itemLabel="#{user.userName}"    itemValue="#{user.userId}" />
</h:selectOneMenu>

Here user is of type UserBean and userList is the list of UserBeans. In the view page it is showing correctly but in the backing bean when i select one item and click on submit button, it showing the selected user as NULL.

My doubt is i can only pass List of SelectItem objects for the f:selectItems or any other beans list ..??

Is there any other way to populate the list of UserBeans otherthan SelectItem for selectItems.

Thank you All, Anil

Anil
  • 41
  • 1
  • 4

1 Answers1

6

You have set the user ID as value of the select items, but you seem to be trying to bind the value to a fullworthy User property in the bean. You need to bind it as an user ID.

<h:selectOneMenu id="users" value="#{MyBean.userId}">
   <f:selectItems value="#{MyBean.userList}" var="user" itemLabel="#{user.userName}" itemValue="#{user.userId}" />
</h:selectOneMenu>

If your sole intent is to be able to select and set User instead of only the ID, then you need a Converter to convert between User and String. That's because HTTP/HTML doesn't understand Java objects. It only understands strings. Here's a kickoff example:

<h:selectOneMenu id="users" value="#{MyBean.user}">
   <f:selectItems value="#{MyBean.userList}" var="user" itemLabel="#{user.userName}" itemValue="#{user}" />
</h:selectOneMenu>

with

@FacesConverter(forClass=User.class)
public class UserConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            return userService.findById(Long.valueOf(value));
        } catch (SomeException e) {
            throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to User", value)), e);
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((User) value).getId());
    }

}

However, this is a pretty expensive job. I'd suggest to stick to passing ID around and obtain the real user in the bean's action method just once instead of letting the converter do it for every single item.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC....What i have to do to bind the full User bean as selected user... – Anil May 26 '11 at 11:20
  • 1
    Use a `Converter`. I have updated the answer with an example. – BalusC May 26 '11 at 11:25
  • Thank you very much Balusc... but i updated my code by placing the userId and one onchange event for my selectOneMenu like this onchange="alert('--'+'document.getElementById('users').value ); it is showing the list of all objects toString values... – Anil May 26 '11 at 11:32
  • Then you were using `itemValue="#{user}"` without a converter. – BalusC May 26 '11 at 11:32
  • With that the form is not submitting and the value which i selected is not there and the default value --Select one--- is showing – Anil May 26 '11 at 11:37
  • That's because you didn't use a converter. Did you understand *anything* of my answer? Did you also read the paragraphs around the code snippets? Use either user `ID` **only** OR use `User` **with** a `Converter`. – BalusC May 26 '11 at 11:37