Question

I am using omnifaces 1.7-snapshot genericEnumConverter. It works as expected. I would like to know if it is possible on the item label to show an enum property instead of its value. I was only able to render the enum itself. Like this:

public enum ErrorType {
    ERR_001, ERR_002, ERR_003;

    private String number;

    static {
        ERR_001.number = "001";
        ERR_002.number = "002";
        ERR_003.number = "003";
    }

    public String getNumber() {
        return number;
    }
}

<f:selectItems value="#{ErrorType}" var="er" itemLabel="#{er.number}" itemValue="#{er}" />
Was it helpful?

Solution

I gather that you're using <o:importConstants> for the <f:selectItems value>? The GenericEnumConverter isn't taking care of this. It plays only a role on converting between the select item value and the component's value. It doesn't play any role on displaying the item labels.

The <o:importConstants> basically makes the enum values available in EL scope as a Map<String, Enum>. Although the <f:selectItems> can take a Map<K, V>, it however doesn't support iterating over a Map<K, V> by var. You need to convert it to a Map.Entry[] first:

<f:selectItems value="#{ErrorType.entrySet().toArray()}" var="er" 
    itemValue="#{er.value}" itemLabel="#{er.value.number}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top