0

I have implemented the following selectOneListBox in jsf

<h:selectOneListbox value="#{category.selectedCategoryId}" onchange="submit()"   
        valueChangeListener="#{category.changeFunction}">
        <f:selectItems value="#{category.categoryValues}" var="cat"
        itemLabel="#{cat.categoryName}" itemValue="#{cat.categoryId}" />
</h:selectOneListbox>

Following is the code for the change listener

public void changeFunction(ValueChangeEvent e) {
    System.out.println(e.getNewValue().toString());
}

As you can see, I can only access itemValue of the selected item in the onchange function.I want to access the object of which itemValue is only a part. How is this possible?

user1107888
  • 1,481
  • 5
  • 34
  • 55

1 Answers1

0

You need to set the value of the select item to be the entire category, something like:

<h:selectOneListbox value="#{category.selectedCategoryId}" onchange="submit()"   
    valueChangeListener="#{category.changeFunction}">
    <f:selectItems value="#{category.categoryValues}" var="cat"
    itemLabel="#{cat.categoryName}" itemValue="#{cat}" converter="catConverter"/>
</h:selectOneListbox>

You will also need a custom converter, which lets a category get serialized between the controller and view.

@FacesConverter("catConverter")
public class CategoryConverter implements Converter {
...
}
dehrg
  • 1,721
  • 14
  • 17