1

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>?

humungs
  • 1,104
  • 4
  • 24
  • 43

2 Answers2

1

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Okay, just to see if I got it right. `selectManyListbox` value refers to `List`, and `selectItems` value refers to `List`. Do I need to implement the `equals()` method in `Status` entity? Do I need to compare with `SelectItem` object inside my entity? Is this right? – humungs Mar 23 '15 at 18:51
  • No. The `SelectItem` has a `Status` as value, right? – BalusC Mar 23 '15 at 22:46
  • Yes! My mistake! You are absolutely right. I implemented the `equals()` method and the `hashCode()` method and it works! Thanks! – humungs Mar 24 '15 at 00:23
0

value in h:selectManyListbox should be subset of itemValue's in f:selectItems.

Create a method which return the List or Array of selected statuses. Use this method in value attribute of h:selectManyListbox. Any value from list/array returned in this method should match itemValue in set of {itemValue, itemLabel} pairs used in f:selectItems.

Vasil Lukach
  • 3,383
  • 3
  • 27
  • 36
  • I do that and I got this error: `java.lang.IllegalArgumentException: Argument Error: An option for component statusMenu was not an instance of javax.faces.model.SelectItem. Type found: java.util.ArrayList.` This ArrayList is na List of `Status` object. – humungs Mar 23 '15 at 19:05