2 Replies Latest reply on Jan 9, 2008 12:00 PM by felixk2

    Creating a simple selectOneMenu

    felixk2

      Hi,

      I'm trying to use a selectOneMenu for a String property in an Entity. For example I have an entity with:

      @NotNull
      @Column(name="Status")
      @Length(max=16)
      private String status;


      Instead of using a textbox for users to fill in this field i'd like to use a drop down with values that I populate. So I created the following h:selectOneMenu:

      <s:decorate template="layout/edit.xhtml">
       <ui:define name="label">Status</ui:define>
       <h:selectOneMenu value="#{customerHome.instance.status}">
       <f:selectItem itemLabel="Active" itemValue="Active"/>
       <f:selectItem itemLabel="Inactive" itemValue="Inactive"/>
       </h:selectOneMenu>
      </s:decorate>


      This works fine for submitting of the form. The selected itemValue will end up in the database. But when I want to view the data the proper selectItem is never selected. I know for example that #{customerHome.instance.status} contains the string "Inactive" but the form shows "Active".

      Any ideas how to fix that?

      Thanks,
      Felix


        • 1. Re: Creating a simple selectOneMenu

          i think this should be possible using s:enumItem and s:convertEnum,
          but you should also consider using a java-enumeration at entity-bean level:

          public enum CustomerStatus {
           INACTIVE,
           ACTIVE;
           public String getLabel() {
           return this.name(); //return whatever name you wanna see
           }
          }

          then use this enum on your entity bean:
           @Enumerated(EnumType.STRING)
           @NotNull
           public CustomerStatus getStatus() {
           return status;
           }
          

          then make the enum values available in the pages via a factory:
          @Name("customerStatusFactory")
          public class CustomerStatusFactory {
          
           @Factory("customerStatus")
           public CustomerStatus[] getCustomerStatus() {
           return CustomerStatus.values();
           }
          }
          

          in your page, use the enum:
          <h:selectOneMenu value="#{customerHome.instance.status}">
           <s:selectItems value="#{customerStatus}" var="cus"
           label="#{cus.label}"
           noSelectionLabel="Please select" />
           <s:convertEnum />
          </h:selectOneMenu>
          

          U may also checkout the examples\ui\src\org\jboss\seam\example\ui example (Person-entity)


          • 2. Re: Creating a simple selectOneMenu
            felixk2

            Thank you very much for that answer. It works perfectly with your method.

            -Felix