4 Replies Latest reply on Jan 27, 2016 1:37 PM by nissrine.nakiri

    h:selectOneMenu not showing selected value, always shows first option

    vinitadhopia

      Hello,


      I have a JSF with a <rich:dataTable>.  A few of the columns contain a <h:selectOneMenu> component.  The problem I'm having is that the value the dropdowns are bound to aren't being selected in the dropdown.  Instead, the dropdowns just default to the first option in the dropdown list.  The datatable has the correct number of rows and the dropdowns have the correct options listed, so I'm not sure what's wrong.



      <rich:dataTable id="personCourseTable" var="_personCourse"
           value="#{personHome.instance.personCourses}"
           rendered="#{not empty personHome.instance.personCourses}">
      
           <h:column>
                <f:facet name="header">
                     <h:outputText value="#{messages['course.fieldLabel.name']}" />
                </f:facet>
                <h:selectOneMenu required="true" value="#{_personCourse.course}">
                     <s:selectItems value="#{courses}" var="_course" label="#{_course.name}"
                             noSelectionLabel="#{messages['course.select']}" />
                     <a4j:support reRender="personCourseOfferings"
                          event="onchange" ajaxSingle="true" />
                     <s:convertEntity />
                </h:selectOneMenu>
           </h:column>
           <h:column>
                <f:facet name="header">
                     #{messages['course.fieldLabel.offering']}
                </f:facet>
                <h:selectOneMenu id="personCourseOfferings" value="#{_personCourse.offering}">
                     <s:selectItems value="#{_personCourse.course.offerings}"
                          var="_offering" label="#{_offering}"
                          noSelectionLabel="#{messages['offering.select']}" />
                     <s:convertEntity />
                </h:selectOneMenu>
           </h:column>
      </rich:dataTable>


        • 1. Re: h:selectOneMenu not showing selected value, always shows first option
          nickarls

          The entity doesn't have equals() and hashcode() implemented?

          • 2. Re: h:selectOneMenu not showing selected value, always shows first option
            vinitadhopia

            I double checked and all the entities involved definitely have their equals() and hashcode() methods implemented.


            FYI, I've added another column containing a <rich:calendar> date component and the data shows up in that component correctly, so I'm still not sure what the problem is.

            • 3. Re: h:selectOneMenu not showing selected value, always shows first option
              vinitadhopia

              I figured it out.  Thank you for the help Nikolas.  After your post, I took a closer look at my equals() methods.  I used Eclipse to generate the equals methods.  There were a couple problems with this.


              First, the equals methods were generated with comparison's to class names.


              public boolean equals(Object obj) {
                   ...
                   if (getClass() != obj.getClass())
                        return false;
                   ...



              This was returning false because the obj.getClass() was returning the class name with $javassist$... appended to it.  I assume this is a reference to my entity's proxy object.  I had to change this to use an instanceof check instead. 


              public boolean equals(Object obj) {
                   ...
                   if (!(obj instanceof Course))
                        return false;
                   ...
              



              I know this is discouraged because a subtype could pass this test, but I'm not sure how else to get around it.


              Secondly, the field comparisons were being done with direct access to the object's fields, rather than using the object's getters.


              public boolean equals(Object obj) {
                   ...
                   final Course other = (Course) obj;
                   if (description == null) {
                        if (other.description != null)
                             return false;
                   } else if (!description.equals(other.description))
                        return false;
                   ...
              


              Since this entity is being lazy loaded, accessing the fields directly were returning default values; null, 0, etc...  I had to change the comparisons to use the other object's getters.


              public boolean equals(Object obj) {
                   ...
                   final Course other = (Course) obj;
                   if (description == null) {
                        if (other.getDescription() != null)
                             return false;
                   } else if (!description.equals(other.getDescription()))
                        return false;
                   ...
              



              Thanks again for pointing me to the equals() methods.


              • 4. Re: h:selectOneMenu not showing selected value, always shows first option
                nissrine.nakiri

                Wow! Your answer was posted in 2009, and now it saved my life ! Thanks