This drives me crazy, cannot find the error.
Here the xhtml page:
...
<h:selectManyListbox style="width: 207px" size="10" value="#{reportBean.selectedSeverities}">
<f:selectItems value="#{reportBean.severities}"/>
</h:selectManyListbox>
...
The report Bean:
...
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 a command Button I have the following action method:
if (!selectedSeverities.isEmpty()) {
Severity s = selectedSeverities.get(0);
}
return;
Wenn I select a severity(enum) and hit the commandbutton I get the following stack trace:
...
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to securityscan.util.Severity
...
I don’t get it.
Any help is very apprecieated.
BR Reen
You can’t use enums in combination with
h:selectMany***components without using a converter. JSF/EL does not see/know the generic type of each of the separate list items. In other words, it only sees aListand notList<Severity>and treats every item as aString, unless you tell it to do otherwise.You need to create and specify a converter yourself. For enums, it’s the best to extend the JSF-provided
EnumConverter.(note that when you’re still using the old JSF 1.2, you should be declaring this as
<converter>infaces-config.xmlinstead of by@FacesConverter)Which you use as follows:
See also: