<h: selectManyListbox JSF and Enums Class Cast error

It drives me crazy, can't find a mistake.

Here is the xhtml page:

...
<h:selectManyListbox style="width: 207px" size="10" value="#{reportBean.selectedSeverities}">
                            <f:selectItems value="#{reportBean.severities}"/>
                        </h:selectManyListbox>
...

Bean Report:

...
private List<Severity> severities;
private List<Severity> selectedSeverities = new ArrayList<Severity>();
...
public List<Severity> getSeverities() {
   if (this.severities == null) {
        this.severities = new ArrayList<Severity>();
        this.severities.add(Severity.LOW);
        this.severities.add(Severity.HIGH);
        this.severities.add(Severity.UNDEFINED);
        this.severities.add(Severity.MEDIUM);
    }
        return severities;
}

For the command button I have the following method of action:

if (!selectedSeverities.isEmpty()) {
    Severity s = selectedSeverities.get(0);
}
return;

Wenn I select severity (enumeration) and click the commandbutton button. I get the following stack trace:

...
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to securityscan.util.Severity
...

I do not understand.

Any help is greatly appreciated.

BR Reen

+3
source share
1 answer

You cannot use enumerations in combination with components h:selectMany***without using a converter. JSF / EL does not see / do not know the general type of each of the individual list items. In other words, he sees only List, and not, List<Severity>and treats each element as Stringif you did not tell him otherwise.

. JSF EnumConverter.

package com.example;

import javax.faces.convert.EnumConverter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="severityConverter")
public class SeverityConverter extends EnumConverter {

    public SeverityConverter() {
        super(Severity.class);
    }

}

( , , JSF 1.2, <converter> faces-config.xml @FacesConverter)

:

<h:selectManyListbox converter="severityConverter">

. :

+7

All Articles