7 Replies Latest reply on Sep 29, 2006 1:20 AM by yj4jboss

    Drop Down Menu Using Seam and Facelets

    jcranwellward

      Can anyone tell how to implement a drop down menu from a database query using seam and facelets?

        • 1. Re: Drop Down Menu Using Seam and Facelets

          Take a look at the Seam wiki. There are a couple third-party selectItems DataBinders there. These will let you take a list returned from your DB and use it within your JSF menu.

          • 2. Re: Drop Down Menu Using Seam and Facelets
            denis-karpov

            http://wiki.jboss.org/wiki/Wiki.jsp?page=SelectItems
            Just added. This one is interesting that it is direct usage without custom annotations.

            • 3. Re: Drop Down Menu Using Seam and Facelets

              Hello Denis,

              I am trying to get your example of implementing SelectItems without custom annotations. I have included the the following code in my SFSB.


              
              
              
              @Stateful
              @Name("companySearch")
              @Scope(ScopeType.SESSION)
              @Interceptors(SeamInterceptor.class)
              
              public class CompanySearchBean implements CompanySearch, Serializable {
              
              
               <! -- Bean Specific Code goes here -->
              
               public Map<String,Object> model;
              
              
               private void fillMaps(){
               model = new TreeMap<String,Object>();
               model.put("(Empty)","-1"); // Add EMPTY element if needed
               for (Object o: yearList) {
               String label = o.toString();
               model.put(label, o );
               }
               }
              
              
               public Map<String,Object> getModel(){ // f:selectItems value
               log.debug("Initializing Model");
               log.debug("Model Size = "+model.size());
               return model;
               }
              
              
              
              
               public Converter getConverter() {
               return new ReferenceConverter(yearList);
               }
              



              This is my code in the my search.xhtml file
              
               <h:selectOneMenu converter="#{companySearch.converter}">
               <f:selectItems value="#{companySearch.model}" />
               </h:selectOneMenu>
              




              Is this the right approach ??

              I am getting a "Cannot get property model" error

              Plz help....and thnx in advance....


              Yogesh







              • 4. Re: Drop Down Menu Using Seam and Facelets
                denis-karpov

                I guess you forgot to declare getter getModel()
                in your CompanySearch interface.

                public abstract Map<String,Object> getModel();


                • 5. Re: Drop Down Menu Using Seam and Facelets

                  Hello Denis,

                  I had declared getModel() in the interface.....I could only get your code to work by doing the following changes:


                  
                   public Map<String,Object> model = new TreeMap<String,Object>();
                  
                   // Instead of public Map<String,Object> model;
                  
                  
                  
                   public void fillMaps(){
                  
                   //model = new TreeMap<String,Object>();
                  
                   model.clear();
                   model.put("2004","-1"); // Add 2004 for test element if needed
                  
                   for (Object o: yearList) {
                   String label = o.toString();
                   model.put(label, o );
                   }
                  
                  
                   }
                  
                  
                  
                   public Map<String,Object> getModel(){ // f:selectItems value
                   log.debug("Initializing Model");
                   log.debug("Model Size = "+model.size());
                   getYearList(); // retrieves all years and places it in yearList
                   fillMaps();
                   return model;
                   }
                  





                  My code in my searchbox.xhtml page is as follows:

                  
                  <h:form>
                  
                  
                   <h:selectOneMenu value="#{companySearch.criterion.year}" converter="#{companySearch.converter}">
                   <f:selectItems value="#{companySearch.model}"/>
                   </h:selectOneMenu>
                  
                   <h:commandButton action="#{companySearch.findCompaniesByYearWithFinancialStatements}"
                   value="Search Company"
                   class="formButton" style="width: 166px;"/>
                  
                  
                  </h:form>
                  
                  


                  Though this works by listing All Years in the drop down menu, there are two problems that i face:

                  (1) value specified in <h:selectOneMenu> is not selected as default
                  ( I tested this with another example)
                  (2) When submitting the form, the submitted value is returned as null

                  2006-09-25 10:49:01,863 DEBUG [accountingAudit.actions.CompanySearchBean] criterion =null
                  


                  However, if i use a plain <h:inputText> to submit this value, the search is carried out correctly.


                  Could you please enlighten me on these two issues ....I am using this for a small application and do not want to use custom annotation as long as your code can get me working ;0)

                  Thnx in advance


                  Cheers,
                  Yogesh

                  • 6. Re: Drop Down Menu Using Seam and Facelets
                    denis-karpov

                    Do not call fillMaps(); in a getter. I think it will solve all of your problems.
                    Good place to call it in the factory method or like that.

                    Then, I think you misunderstand this line

                    model.put("(Empty)","-1"); // Add EMPTY element if needed

                    It is EMPTY element. It is needed to choose null value. If you do not need null values just remove this line.


                    • 7. Re: Drop Down Menu Using Seam and Facelets

                      Hello Denis,
                      Could you please tell me what the difference between using your way of implementing SelectItems, the Custom Annotations way and simply using tomahawk to implement SelectItems.

                      Cheers,
                      Yogesh