1 Reply Latest reply on Apr 12, 2007 10:22 AM by Samuel Mendenhall

    EnumConverter

    Boris Temkin Newbie

      Hello sirs.

      I try call page someAction.xhtml that mapped to seam component someAction. Everything fine except of enum convertion.

      enum TypeEnum {
      type1
      }
      


      @Stateless
      @Name("someAction")
      class SomeActionBean implements SomeAction {
       private long id;
       private TypeEnum type;
      
       public void method() {
       ....
       }
       ....
      }
      


      pages.xml:
      <page view-id="/someAction.xhtml" action="#{someAction.method}">
       <param name="id" value="#{someAction.id}"/>
       <param name="type" value="#{completeAction.type}"/>
      </page>
      


      When I call url
      http://localhost:8080/myapp/someAction.seam?id=2&type=type1
      


      logs from SomeAction.method shows that id= 2 but type is null (!)

      what I am doing wrong?
      The only one way that I found to work around is following:

      1. implement javax.faces.convert.Converter intrface in my own class.
      2. add to faces-config lines:
       <converter>
       <converter-id>MyConverter</converter-id>
       <converter-class>mypakage.MyEnumConverter</converter-class>
       </converter>
      </faces-config>


      3. change pages.xml to
      <page view-id="/someAction.xhtml" action="#{someAction.method}">
       <param name="id" value="#{someAction.id}"/>
       <param name="type" value="#{completeAction.type}" converterId="MyConverter"/>
      </page>
      


      It is work, but (IMHO) it is ugly.

      I found that seam already has some EnumConverter, so i can use it.
      Please, point me to appropriate resources, where I can clerify the question.

      Thank you.

      PS: I use seam 1.2.0.PATCH1


        • 1. Re: EnumConverter
          Samuel Mendenhall Apprentice

          Use seams built in enum handling:

          Here is an example of handling a True/False Enum

          <h:outputLabel for="active">
           Active
           <span class="required">*</span>
           </h:outputLabel>
           <s:decorate id="activeDecoration">
           <h:selectOneMenu id="active" value="#{testHome.instance.active}">
           <s:convertEnum />
           <s:enumItem enumValue="TRUE" label="True" />
           <s:enumItem enumValue="FALSE" label="False" />
           </h:selectOneMenu>
           </s:decorate>
          



          public enum BooleanEnum {
           TRUE("TRUE"),
           FALSE("FALSE");
           private final String name;
          
           /**
           * Prevent instantiation and subclassing with a private constructor.
           */
           private BooleanEnum(String name) {
           this.name = name;
           }
          
           private static final Map INSTANCES = new HashMap();
          
           static {
           INSTANCES.put(TRUE.toString(), TRUE);
           INSTANCES.put(FALSE.toString(), FALSE);
           }
          
           // ********************** Common Methods ********************** //
          
           public String toString() {
           return name;
           }
          
           Object readResolve() {
           return getInstance(name);
           }
          
           public static JobStatus getInstance(String name) {
           return (JobStatus) INSTANCES.get(name);
           }
          }
          


          And in the entity:

          @Enumerated(EnumType.STRING)
           @Column(name="ACTIVE")
           @NotNull
           public BooleanEnum getActive() {
           return active;
           }
           public void setActive(BooleanEnum active) {
           this.active = active;
           }