6 Replies Latest reply on Aug 13, 2009 9:41 PM by Jenny Jasti

    display of data in combobox

    Jenny Jasti Newbie

      Hi All,


      I am new to Seam and even JSF and trying to display the data using h:selectOneMenu tag. I can access the data in the bean class using the following code



      @Factory("workflowData")
          public void getWorkflowData()
          {           
              workflowData = em.createQuery("from RpmUser u where u.userName like :username1")
              .setParameter("username1", "a%" )
              .getResultList();       
               for(RpmUser us1: workflowData)
               {              
                      for(UserJournal uj: us1.getUserJournals())
                      {
                              log.info("Journals"+ uj.getId().getJournal());
                      }
                      
               }
           
          }



      I have three entities RpmUser, RPMJournal, UserJournal. RpmUser entity has the following code



      @OneToMany(fetch = FetchType.LAZY, mappedBy = "rpmUser")
              public Set<UserJournal> getUserJournals() {
                      return this.userJournals;
              }
      
              public void setUserJournals(Set<UserJournal> userJournals) {
                      this.userJournals = userJournals;
              }



      I have UserJournalId class as UserJournal has composite keys. It has the following code:



      @Embeddable
      public class UserJournalId implements java.io.Serializable {
      
              private String journal;
              private BigDecimal userId;
      
              public UserJournalId() {
              }
      
              public UserJournalId(String journal, BigDecimal userId) {
                      this.journal = journal;
                      this.userId = userId;
              }
      
              @Column(name = "JOURNAL", nullable = false, length = 3)
              @NotNull
              @Length(max = 3)
              public String getJournal() {
                      return this.journal;
              }
      
              public void setJournal(String journal) {
                      this.journal = journal;
              }
      
              @Column(name = "USER_ID", nullable = false, precision = 22, scale = 0)
              @NotNull
              public BigDecimal getUserId() {
                      return this.userId;
              }
      
              public void setUserId(BigDecimal userId) {
                      this.userId = userId;
              }



      Can some one help me how to display the list of journals for a particular user in the front end, i tried but was not successful


      Thanks.







              

        • 1. Re: display of data in combobox
          Ryan Kimber Newbie

          You need to create a method that will return a List or Array of SelectItem objects. These objects will be populated into a selectOneMenu element in your .xhtml.


          The SelectItem constructor takes two arguments, one is the String that will be displayed in the drop-down, the other is the value object. To make things easy, I typically pass a id string as the value object instead of the object itself.


          In your .xhtml you need to create an selectOneMenu element. That element's value attribute will be bound to your seam component via value="#{yourBeanName.yourListMethod()}".


          A good resource for how to do this in JSF is the ICEFaces component showcase. They have plenty of examples (although they aren't particular to Seam).

          • 2. Re: display of data in combobox
            Jenny Jasti Newbie

            I am accessing workflowData list in .xhtml page, it is the list of users and as it contains list of journals in the RpmUser entity bean, can't I access the list with out writing a separate method i.e.


            workflowData.getUserJournals() gives me the list of journals for that user, but it doesn't have the journals instead have the id's of UserJournalId class and this class have the journals of that user.


            I tried the following code and it doesn't work. I know I am missing some thing


            <h:selectOneMenu value="#{workflowUsers.userJournals.id}">
                 <f:selectItems
                    value="#{workflowUsers.userJournals.id.journal}" />
            </h:selectOneMenu> 



            I am not even sure if we can do this way. Please let me know if I can achieve this using the same list(RpmUser's) that I have retrieved.


            Thanks.

            • 3. Re: display of data in combobox
              Ryan Kimber Newbie

              I may be wrong - I'm fairly new to Seam and JSF myself, however, my understanding is that the selectOneMenu components require and array or List of SelectItem objects, that is, you do have to provide a method that returns List<SelectItem> or SelectItem[].


              If I'm wrong, I hope someone will correct me, because I'd like to be able to do this without any intermediary code, but as it stands, I create an instance of a helper class that wraps my 'selectable' objects, and I refer to that instance from my XHTML markup.

              • 4. Re: display of data in combobox
                Ben Utzer Newbie

                Hi,


                You should be able to use the s:selectItems component together with s:convertEntity as it is used in the Seam examples (searchbox.xhtml of the dvdstore example for example).
                It automatically converts Lists or Arrays to SelectItems. The only problem is that you cannot use a Set directly but must convert it to a List.


                Cheers,


                Ben

                • 5. Re: display of data in combobox
                  Jenny Jasti Newbie

                  Is it possible when I use EmbeddedId in the entity, i.e. my userJournal table has composite keys.


                   workflowData = em.createQuery("from RpmUser u where u.userName like :username1")
                          .setParameter("username1", "a%" )
                          .getResultList();       
                           for(RpmUser us1: workflowData)
                           {              
                                  for(UserJournal uj: us1.getUserJournals())
                                  {
                                          log.info("Journals"+ uj.getId().getJournal());
                                  }
                                  
                           }
                  



                  I can say my workflowData list has a list of ID class objects which in turn contains the list of journals.


                  Thanks.


                  • 6. Re: display of data in combobox
                    Jenny Jasti Newbie

                    When I did the following it gave me a list of JournalId obects


                                  <h:selectOneMenu value="#{workflowUsers.userJournal}">
                                    <s:convertEntity />
                                      <s:selectItems value="#{workflowUsers.userJournals}"
                                          var="jour"
                                          label="#{jour.id}" noSelectionLabel="Journal list"/>
                                  </h:selectOneMenu>



                    I tried to change the label to {jour.id.journal} then it displayed nothing and not even an error. Can some one tell me how to retrieve the column data in Embeddable class into seam


                    Thanks.