Frage

Ich mag Satz Elemente aus einer Liste zu dem selectonemenu in ICEfaces. Aber wenn ich das gleiche tun bekomme ich folgende Fehlermeldung: java.lang.ClassCastException: kann nicht auf javax.faces.model.SelectItem gegossen werden

Das ist eine Entität-Klasse.

Bitte Hilfe.

War es hilfreich?

Lösung

The normal way of creating and populating the selectOneMenu items would be the following:

private String selectedItem; // +getter +setter
private List<SelectItem> selectItems; // +getter

public Bean() {
    selectItems = new ArrayList<SelectItem>();
    for (Entity entity : getYourEntities()) {
        selectItems.add(new SelectItem(entity.getValue(), entity.getLabel()));
    }
}

With the following in the view (you can easily subsitite <h: with <ice:):

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItems value="#{bean.selectItems}" />
</h:selectOneMenu>

Instead of a String value, you can also use any Number (Integer, Long, etc) since JSF has builtin converters for this. But if you want to use whole objects as item value, then you need to create a Converter. This is described in detail in this article.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top