0

I followed the advice from How to use enum values in f:selectItem(s) to build a h:selectOneMenu that takes the values of an enum. This works fine until I reload the page.

If I reload the page (Strg+R) what a user could do, the #{bean.relationship} property becomes null. All other properties, such as strings, numbers, etc. remain as they were before (Bean is @ViewScoped and @ManagedBean).

Here is the code from the JSF:

<h:selectOneMenu value="#{bean.relationship}">
    <f:selectItems value="#{bean.relationshipTypes}" 
        var="types" itemValue="#{types}" itemLabel="#{types}" />
</h:selectOneMenu>
<h:inputText value="#{bean.name}" />

Here the code from the Enum:

public enum RelationshipType {
   Family,
   Friend    
}

Here the code from the Bean:

private RelationshipType relationship; // plus getter & setter
private RelationshipType[] relationshipTypes;
private String name; // plus getter & setter

public RelationshipType[] getRelationshipTypes() {
    return RelationshipType.values();
}

The enum is part of a larger entity. For ease of display a shortend version. Any idea?

@Geinmachi: Yet, other values stored in the bean are still there after the reload (e.g. name). So, why not for the property related to the enum?

Community
  • 1
  • 1
Marcus
  • 161
  • 1
  • 2
  • 12

1 Answers1

-1

I have used enums for data types

Enum

public enum Datatypes {
INT("Int"), FLOAT("Float"), DOUBLE("Double"), STRING("String"), DATE("Date"), DATETIME(
        "DateTime"), BLOB("Blob");

private final String dataType;

private Datatypes(String dataType) {
    this.dataType = dataType;
}

public String getDataType() {
    return this.dataType;
}

@Override
public String toString() {
    return dataType;
}

}

Bean

private List<Datatypes> datatypes = Arrays.asList(Datatypes.values());

public List<Datatypes> getDatatypes() {
    return datatypes;
}

public void setDatatypes(List<Datatypes> datatypes) {
    this.datatypes = datatypes;
}

xhtml

 <p:selectOneMenu value="#{bean.dataType}">
 <f:selectItem itemLabel="Select Data type"/>
 <f:selectItems value="#{bean.dataTypes}" />
 </p:selectOneMenu>

it working for me.

subhakar patnala
  • 319
  • 1
  • 4
  • 15