5 Replies Latest reply on Apr 9, 2007 11:41 AM by pmuir

    Drop Down Lists using Facelets?

    jcranwellward

      I have a list of Java Entity Beans and I want to be able to display an attribute of the beans in a drop down style list box so once the user selects that entity, the reference for that object is stored in parent object.

      I'm not sure how to implement this, has any body done this before?

      Thanks for the help,

      James

        • 1. Re: Drop Down Lists using Facelets?

          Assuming Parent and Child entities where Child has a reference to parent.

          <h:selectOneMenu value="#{child.parent}">
           <s:selectItems value="#{allParents}" var="parent" label="#{parent.display}"/>
          </h:selectOneMenu>
          


          Tweak names as necessary. You'll need some converter in conjunction with this to convert an entity to a string and back. Look at the seam entity converter for this, I've not used it myself.

          HTH.

          Mike.


          • 2. Re: Drop Down Lists using Facelets?
            jark

            Hi,

            i have done something similair in a schoolproject of mine. In this project we had a user who was a member of a certain organisation. On the useredit page you had to be able to change the organisation of the user by using an dropdown list. The big problem was to let it see a name rather than an organisation reference it self, we used an converter for this as described by quilleashm.

            First the dropdownlist in the edit page:

            <h:selectOneMenu value="#{userHome.instance.orgLevel}"
             converter="#{orgbean.converter}">
             <f:selectItems value="#{orgbean.orgLevels}" />
             </h:selectOneMenu>


            Second the converter:
            import java.io.Serializable;
            import java.util.List;
            import java.util.Map;
            import java.util.TreeMap;
            
            import javax.ejb.Remove;
            import javax.ejb.Stateful;
            import javax.faces.component.UIComponent;
            import javax.faces.context.FacesContext;
            import javax.faces.convert.Converter;
            import javax.faces.convert.ConverterException;
            import javax.persistence.EntityManager;
            import javax.persistence.PersistenceContext;
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Create;
            import org.jboss.seam.annotations.Destroy;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            
            
            @Stateful
            @Name("orgbean")
            @Scope(ScopeType.EVENT)
            public class OrgLevelsBean implements OrgLevels, Serializable
            {
             List<OrgLevel> orgLevels;
             Map<String,OrgLevel> orgLevelMap;
            
             @PersistenceContext
             EntityManager em;
            
             @Create
             public void loadData() {
             orgLevels = em.createQuery("select o from OrgLevel o")
             .setHint("org.hibernate.cacheable", true)
             .getResultList();
            
             Map<String,OrgLevel> results = new TreeMap<String,OrgLevel>();
            
             for (OrgLevel orgLevel: orgLevels) {
             results.put(orgLevel.getName(),orgLevel);
             }
            
             orgLevelMap = results;
            
             }
             public Map<String, OrgLevel> getOrgLevels() {
             return orgLevelMap;
             }
            
             public Converter getConverter() {
             return new OrgLevelConverter(orgLevels);
             }
            
             public OrgLevel getNullOrgLevel() {
            
             return new OrgLevel();
             }
            
             static public class OrgLevelConverter implements Converter,Serializable
             {
             List<OrgLevel> orgLevels;
            
             public OrgLevelConverter(List<OrgLevel> orgLevels) {
             this.orgLevels = orgLevels;
             }
            
             public String getAsString(FacesContext facesContext,
             UIComponent component,
             Object obj)
             {
             if (obj == null) return null;
             OrgLevel orgLevel = (OrgLevel) obj;
            
             String val = String.valueOf(orgLevel.getId());
             return val;
             }
            
             public Object getAsObject(FacesContext facesContext,
             UIComponent component,
             String str)
             throws ConverterException
             {
             if (str == null || str.length()==0) {
             return null;
             }
            
             int id = Integer.valueOf(str).intValue();
             for (OrgLevel orgLevel : orgLevels) {
             if (orgLevel.getId() == id) {
             return orgLevel;
             }
             }
            
             return null;
             }
            }
             @Remove @Destroy
             public void destroy() {}
            
            
            }
            


            I think we largely took this from an example included in the Seam package but i'm not sure. Hope this helps :)

            • 3. Re: Drop Down Lists using Facelets?
              jcranwellward

              Thanks alot thats exactly what I need.

              Cheers,

              James

              • 4. Re: Drop Down Lists using Facelets?
                waynebagguley

                If you are using Seam v1.2.1.GA or above you can use this instead:

                <h:selectOneMenu value="#{itemManager.itemChosen}" style="width:300px">
                 <s:selectItems value="#{itemManager.itemList}" var="item"
                 label="#{item.code} #{item.name}" />
                 <s:convertEntity />
                </h:selectOneMenu>
                


                This negates the need for a custom converter implementation as long as:


                The 'item' is an Entity bean
                Seam manages the persistence context
                The equals method of the entity bean returns true if it matches the primary key fields of the comparing bean




                • 5. Re: Drop Down Lists using Facelets?
                  pmuir

                   

                  "waynebagguley" wrote:
                  <ul>
                  <li>The equals method of the entity bean returns true if it matches the primary key fields of the comparing bean</li>
                  </ul>


                  Either that, or ensure that the same persistence context is used for loading the list, and then reloading the selected item - this is easy to do by making sure you have a conversation that spans the list load and the update/persist of the entity.