3 Replies Latest reply on Feb 25, 2010 3:47 PM by kblief.kblief.gmail.com

    Custom Converter

      Hello,


      please bear in mind that I'm a newbie towards both JSF/Facelets and Seam.


      I'm a bit puzzled towards the usage of Converters.
      A short description of what is sure to be a recognizable situation for many of you.


      I got my own 'user.java' bean, with a 'name', 'firstname', 'login', 'password' etc... The basic user bean.


      I wish to create a selectOneMenu where the entries in the dropdown are based on the users in my database.
      I'm perfectly capable of getting the list of users into my webapp, but I'm at a complete loss as how to show the users in my selectOneMenu.


      What I wish is for my selectOneMenu to show as entries the string(s) that are composed of the firstname and lastname of each of my users that I get from the list in the database.


      I've been searching google for hours, perhaps I'm searching with wrong/bad parameters but I'm ready to throw in the towel...



      Anyone able to point me in the right direction?


      regards,
      Pieter

        • 1. Re: Custom Converter
          daxxy

          Hi Pieter I'm in exactly the same boat as you are, however, I am starting to get a glimmer of understanding regarding custom converters.


          My dilemma is I wish to use a richfaces comboBox instead of a selectOneMenu.


          BTW -- as I'm writing this it occurs to me that you don't need a converter if you are using selectOneMenu. You can use s:selectItems to display values and labels based on your database query.


          Here is some code I used to display a selectOneMenu containing entries that look like this




          AK (Alaska)
          AL (Alabama)





          But the value that gets input is simply the 2 character abbreviation, ie AK or AL




          <h:selectOneMenu value="#{office.state}">
            <s:selectItems noSelectionLabel=" "
              value="#{statesList.stateNames}" var="_state"
              label="#{_state[0]} (#{_state[1]})"
              itemValue="#{_state[0]}" />
          </h:selectOneMenu>





          Let me know if you need more details.  The basic is that what you display is the value of label and what you input is the value of itemValue.


          http://in.relation.to/Bloggers/SselectItemsCustomisingTheValue

          • 2. Re: Custom Converter

            Either use <s:convertEntity> (see section 33.1.2.2 of the Seam documentation) or write your own converter (section 33.2 of the Seam documentation).

            • 3. Re: Custom Converter
              kblief.kblief.gmail.com

              You can use s:convertEntity like so


              <h:selectOneMenu value="#{someBean.user}" id="utoL"  >
                   <s:selectItems value="#{userBean.users}" 
                         var="user" 
                         label="#{user.firstname} #{user.lastName}"
                         noSelectionLabel="None"/>
                   <s:convertEntity/>
              </h:selectOneMenu>



              If you use s:convertEntity you have to over ride the equals and hashcode methods of you entity.  It can be something as simple as :


              public boolean equals(Object obj) {
                  boolean equals = false;
                  if (obj != null
                      && obj instanceof User
                      && ((User) obj).userId == this.userId) {
                   
                      equals = true;
                  }
                        
                  return equals;
              }