Search code examples
jsfrichfacesjsf-1.2

How to pre-select data in h:selectManyListbox


This is a common problem and I already saw the basic solution (populate the selectManyListBox). Here is the code of my JSF 1.2 page:

<h:selectManyListbox id="statusMenu" converter="statusConverter" value="#{aprvAction.ap.statuses}" >
    <f:selectItems id="statusItem" value="#{action.ap.statusItens}"/>
    <a:support event="onclick" ajaxSingle="true" immediate="true" eventsQueue="queueGeral" />
    <a:support event="onchange" ajaxSingle="true" eventsQueue="queueGeral" process="statusMenu"/>
</h:selectManyListbox>

The thing is that #{aprvAction.ap.statuses} is an instance of List<Status> class. However, in tag <f:selectItems> the value: #{action.ap.statusItens} is an Instance of List<SelectItem>.

I populate the the #{aprvAction.ap.statuses} with the values that I want to pre-select the ListBox, but did not work. I think it's because they are different objects in <selectManyListBox> and <selectItems>.

How can I solve this, and show the pre-selected values in <selectManyListBox>?


Solution

  • JSF will use equals() method to compare available items with (pre)selected items.

    In case of standard objects, such as String, this is already implemented.

    In case of custom objects, such as your Status entity, it's your responsibility to make sure that you've properly implemented the equals() (and hashCode()) method in the class.

    See also: