Question about SelectItems

Hello

when I have the following Code:

<h:selectManyListbox value="#{bean.mySelectedItems}">

<f:selectItems value="#{bean.mySelectItems}" />

</h:selectManyListbox>

and in my Managed Bean:

public void setSelectedItemsLeft(List selectedItemsLeft) {

if (selectedItemsLeft.size() > 0) {

System.out.println("NEW:" + selectedItemsLeft.get(0).getClass().getName());

}

this.selectedItemsLeft = selectedItemsLeft;

}

why is the class name "String" and not "SelectItem ?

I tried to uses a method signature like this:

public void setSelectedItemsLeft(List<SelectItem> selectedItemsLeft)

and run only into trouble.

for example when I wanted to call the code above this line:

System.out.println("NEW:" + selectedItemsLeft.get(0).getClass().getName());

gave me a ClassCastExeption.

I always thougth that there is a List of SelectItems injected into the

Managed Bean and not a List of Strings.

can anyone make this clear for me ? I found several confusing explanations on the

web.

Claus

[1142 byte] By [ClausHa] at [2007-11-26 15:37:47]
# 1

Claus,

Your getting String because JSF doesn't return the full SelectItem object. It only returns the value of the SelectItem object.

The SelectItem object has a value and a label. When JSF reads each SelectItem from the list, it displays the label in the selectManyListbox (for the user to read). Then the user selects one or more items from the listbox. When the form is submitted, JSF then builds a new list of the selected items. The list gets populated with the value portion of the SelectItem that was selected. It does not get populated with the actual SelectItem object.

Hence, you get a List of String objects in your setSelectedItemsLeft.

BTW, the method naming in your example is wrong. I think you meant to add a "Left" in it. Like this:

<h:selectManyListbox value="#{bean.mySelectedItemsLeft}">

<f:selectItems value="#{bean.mySelectItems}" />

</h:selectManyListbox>

Anyways, that's probably beside the point.

CowKing

PS - I don't want to confuse you, but JSF could actually set a list of any object type. The SelectItem value can be any Object. Therefore, you could just as easily make the value an Integer, Boolean, ArrayList, StringBuffer, etc... etc...

IamCowKinga at 2007-7-8 21:55:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If you understand generics, this declaration explains a lot:

private List < T > mySelectedItems;

private List < SelectItem < T, String > > mySelectItems;

// think away the spaces, the UBB parser of this forum doesn't like the < and > tags ..

T = the Object Type you want to pass through.

String = the label you see in the listbox.

BalusCa at 2007-7-8 21:55:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...