Enum with JSF and Primefaces

Overiew

  • value=”#{editor.myEnumValues} returns an array of all enum Members
  • itemValue=”#{status}” changes genderEnum parameter via setGenderEnum(GenderEnum genderEnum)
  • itemLabel=”#{backend[status]} references a property file doing some Translations
  • <p:ajax process=”@this” .. > updates the DOM elements msg3 and myTextOut3 on every value change
  • Local messages from menu3 [ like value validation errors ] are send to < h:message … > Element

xhtml File


        <p:panelGrid columns="2" layout="grid" size="300"  style="border:2;" >
            <h:selectOneMenu id="menu3" required="true" value="#{editor.genderEnum}"
                             layout="pageDirection"
                             style="width:250px"  >
                <p:ajax process="@this" update="myTextOut3 msg3" />

                <f:selectItem itemValue="#{null}" itemLabel="Auswahl Geschlecht" noSelectionOption="true"  itemDisabled="true" />
                <f:selectItems
                        value="#{editor.myEnumValues}"
                        var="status"
                        itemValue="#{status}"</strong>
                        itemLabel="#{backend[status]}"
                />
            </h:selectOneMenu>
            <h:message id="msg3" for="menu3" />

            <h5>JSF selectOneMenu Status:</h5>
            <h:outputText id="myTextOut3" value="#{editor.genderEnum}" required="true" style="width: 120px;  position: relative; top: 50%; transform: translateY(-50%);"></h:outputText>

        </p:panelGrid>

Java File

  • getMyEnumValues returns all

..
    public GenderEnum getGenderEnum() {
        return genderEnum;
    }

    public void setGenderEnum(GenderEnum genderEnum) {
        this.genderEnum = genderEnum;
    }

    public GenderEnum[] getMyEnumValues() { return genderEnum.values(); }
..

Property File backend.properties


# User
FEMALE=Frau
MALE=Herr

Reference: