3 Replies Latest reply on May 20, 2008 10:10 AM by lcurros

    Pre Select selectOneMenu

      Hi!! I'm working with JBOSS SEAM 2.0.1.GA plus EJB 3.


      I'm a bit confusing with selectItems and converters. I have a page where I select one element from a combo (typical foreign key). So my xthml code is something like this:



      .............
      .............
      <h:selectOneMenu value="#{myBean.myBeanType}">
        <s:selectItems value="#{beanTypes.resultList}" var="type" 
                       label="#{type.name}" 
                       noSelectionLabel="Please Select..."/>
          <s:convertEntity />
      </h:selectOneMenu>
      .............
      .............




      This combo is used to filter the results of a search, so I need to maintain selected the correct value after submits. So I have one page param:


      <param name="type" value="#{myBean.myBeanType}"




      But nothing is preselected. So I thought the problem was the need of converter in param. So I put one converter in the param:


      <param name="type" value="#{myBean.myBeanType}" converterId="org.jboss.seam.ui.EntityConverter"



      But nothing is preselected again. However now I can see the param in the URL with a not null value. What's wrong in my approach?


      Thanks in advance

        • 1. Re: Pre Select selectOneMenu
          nickarls

          The item is selected when it's value matches the one where it's headed (the selectOneMenu value).


          The matching is done with equals() and hashCode() in your entities when convertEntity is used. Override them. Use a SMPC. Drop any extra parameters etc.


          Wow. That sounded authoritative for a guy just guessing ;-)

          • 2. Re: Pre Select selectOneMenu

            Thanks Nicklas!


            I tried to redefine equals()


                 @Override
                 public boolean equals(Object obj) {
                      if (obj == null)
                           return false;
                      else if (!(obj instanceof BeanType))
                           return false;
                      else if (((BeanType) obj).getBeanTypeId().equals(
                                this.beanTypeId))
                           return true;
                      else
                           return false;
                 }




            and hashCode() as you said



                 @Override
                 public int hashCode() {
                      int result = 17;
            
                      result = 37 * result + (int) this.getBeanTypeId().intValue();
                      return result;
                 }



            But is still not preselecting anything.
            Is correct this declaration of param and converterId to use in combination with s:converEntity?


            <param name="type" value="#{myBean.myBeanType}" converterId="org.jboss.seam.ui.EntityConverter"/>





            • 3. Re: Pre Select selectOneMenu

              I couldn't solve this problem, I'm not sure why. Its strange because I was not getting errors.


              But I found a workaround, I add a new property to my Search Action so now I have two.


              Both properties store the selectedBeantype




              1. BeanType searchBeanType

              2. Long selectedBeantTypeId





              So my selectOneMenu is basically the same


              .............
              .............
              <h:selectOneMenu value="#{searchAction.searchBeanType}">
                <s:selectItems value="#{beanTypes.resultList}" var="type" 
                               label="#{type.name}" 
                               noSelectionLabel="Please Select..."/>
                  <s:convertEntity />
              </h:selectOneMenu>
              .............
              .............



              But the page param is different to selectOneMenu value. This is the change. (maybe the converterId I was using didn't work as I want)



              <param name="type"   value="#{searchAction.selectedBeanTypeId}" />



              The getter and setter for this property are the attached. For searcBeanType property are the standard.


                   public Long getSelectedBeanTypeId() {
                        return searchBeanType != null ? searchBeanType.getBeanTypeId() : null;
                   }
              
                   public void setSelectedBeanTypeId(Long id) {
                        this.busquedaArea = entityManager.find(BeanType.class, id);
                   }



              With this approach the preselection of combo works. Maybe is not the best solution but works. If anybody can help me with the previous approach please reply the post!!thanks