2 Replies Latest reply on Jan 3, 2008 5:55 PM by menashe

    Accessing Enum with EL

    menashe

      How do i access Enum from EL
      here is enum

      package mypackage;
      public enum Status {GOOD, BAD}

      here the object
      package mypackage;
      @Name("user")
      public class User {
       private Status status;
      
       public Status getStatus()
       {return status;}
      
       public void setStatus(Status status)
       {this.status=status;}

      and here is broken EL
      <h:outputText value="all is good" rendered=#{user.status == Status.GOOD}"/>

      anyone has an idea how to get it to work?

        • 1. Re: Accessing Enum with EL
          pmuir

          EL doesn't support accessing enums like that. You would need to write an EL function I think.

          • 2. Re: Accessing Enum with EL
            menashe

            that is what i do now but this is not very efficient since it had to create enum object from string all the time (i can add each enum constant to the named class but it can be tedious when there are lots of enums)

            public boolean isGood(final String value) {
             return status.equals(value);
             }

            and this has to be added to each enum class (with Status.class updated to reflect the actuall class)
            public boolean equals(final String value) {
             if (value == null || value.length() == 0) {
             return false;
             }
             try {
             if (this == Enum.valueOf(Status.class, value)){
             return true;
             }
             } catch (final IllegalArgumentException iae) {
             return false;
             }
             return false;
             }