Announcement Announcement Module
No announcement yet.
form:option not binding to my bound value in form:select Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:option not binding to my bound value in form:select

    I have a list of Semester objects that I want to display in a drop down box. Next to the drop down box is a button to refresh the screen based on Semester selected. When the screen first displays it does not have option that matches my bound value marked as selected. When the user selects the semester and presses the button I get the semester the user has selected and process it. But when the dropdown box is displayed again the semester selected is still not displayed as selected.

    I have created a simple property editor with getAsText and setAsText and my logging tells me that it is binding to my command object correctly, but shouldn't that semester be in my dropdown box as selected?

    <form:form id="formBox" commandName="student">
         <form:select cssClass="term" path="semester">
               <form:options items="${ student.semesterList.semesters }"  />
         <button type="submit" name="changeTerm">Change Term</button>
    I thought of adding a forEach inside my select and looking for a match to my bound value and then mark it as selected but the form:option tag does not have a selected value.

    Thanks for any insight or documentation that would clear this up. I have reviewed the spring documentation and it is brief and did not help me figure out this problem.

  • #2
    working using &lt;option&gt; instead of &lt;form:options&gt;

    Got it to work using a combination of spring tag and html.
    <form:select cssClass="term" path="semester">
       <c:forEach items="${student.semesterList.semesters}" var="sem" varStatus="cnt">
                 <c:when test="${sem.termDesc == student.termDesc}">
                      <option value="${sem.termDesc}" selected="selected"> ${sem.termDesc}</option>
                       <option value="${sem.termDesc}">${sem.termDesc}</option>
    My propertyeditor seems to be working correctly, but it might be that the spring tags just can't handle this kind of object.


    • #3
      in the same boat 2+ years later

      I know this is an old thread, but have we made any progress with form binding since 2007? I ran into the same issue today.

      I make a selection, post it to the AbstractWizardFormController, (elsewhere I force validation to fail so I'll return to the same page,) and when I return to the page, my selection is not selected. I do know that the form backing object holds the selected value because I can print out the value from the form backed object.

      I also wrote my own object that extends PropertyEditorSupport and bind it within the initBinder method.

      How frustrating!


      • #4
        Make sure that you have a valid equals method on the object that you are displaying in the options. Also make sure you use the itemValue property of the form:select or the form:options tag set.

        I use the form:select tag all the time and it works like a charm.


        • #5

          Thanks for the reply. That did it.


          Thanks to another <a href="">thread</a> I realized I needed to @Override the equals method on my object. I was populating the list originally with JSON data (AJAX), then storing it in the refData of the controller. By creating my own equals method (comparing id's) I have solved the problem. Thanks!