h:selectManyListbox. javax.faces.SelectItem and the fact the selected items always returned as a List<String>

Just today I discovered that by default, whatever selected in a multi listbox, h:selectManyListBox always returns List<String>.

Imagine the java class looks like this:

public class Bean implements Serializable {
    public List<SelectedLongs> selectedLongs;
    // accessor and mutator of property 'selectedLongs'

    public List<SelectItem> getLongOptions() {
        final List<SelectItem> options = new ArrayList<SelectItem>();
        options.add(new SelectItem(1l, "1");
        options.add(new SelectItem(2l, "2");
        options.add(new SelectItem(3l, "3");

And, the following XHTML for JSF display:

<h:selectManyListbox size="5" value="#{bean.selectedLongs}">
    <f:selectItems value="#{bean.longOptions}" />

Surprisingly, what received during submit is not a List<Long>, but List<String>.

In order to fix this, remember to add converter="javax.faces.Long" as shown below:

<h:selectManyListbox size="5" value="#{bean.selectedLongs}" converter="javax.faces.Long">
    <f:selectItems value="#{bean.longOptions}" />

BalusC, 2010, ‘JSF 2.0 use enum in selectMany menu’, accessed 03 February 2012