Announcement Announcement Module
No announcement yet.
form:option tag not generating "selected" attribute Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:option tag not generating "selected" attribute

    I have a problem with the <form:option/> tag. It does not generate the selected="selected" attribute. As comparison, the following works perfectly:

    <form:select multiple="true" path="event.speakers" id="speakers">
    <form:options items="${speakers}" itemValue="id" itemLabel="lastName"/>

    It generates:

    <option selected="selected" value="1039">Thompson</option>

    However, for my needs I need to show a more complicated label (not just the last name but also the first name). Thus, seemingly the <form:option/> tag should be a good alternative (as I can created a more complicated label using EL).

    The following code snippet in my opinion should generate the same html output as above. I have the following code using the <form:option/> tag:

    <form:select multiple="true" path="event.speakers" id="speakers">
    <c:forEach items="${speakers}" var="s">
    <form:option value="${}" label="${s.lastName}" />

    Everything is generated fine, except the selected="selected" attribute is NOT generated.

    What do I miss here?

    I use Spring 3.0.4.RELEASE and I also tried investigating ways to work around this issue thinking that I may need custom Converters (implementing org.springframework.core.convert.converter.Convert er) but it did not seem to have any impact.

    I started looking through the source code a bit. The code is hitting org.springframework.web.servlet.tags.form.Selected ValueComparator. And the evaluation fails because boundValue (String, comma separated String of Ids) and candidateValue (A Long value) don't match.

    The question is: Why does the form:option tag fail while the form:options tag just works flawlessly? Do I have to create an "old-school" property editor?

    If you have any helpful hints, please let me know!


  • #2
    I'm just guessing, but can it be that event.speakers is a array/list of Strings and is a Long?
    If so can't you make event.speakers an array/list of Longs as well?


    • #3
      I know this topic is 6 months old but I had the same problem and couldn't solve it. I had to "fall back" to form:options.
      But I've found a post which may help: link

      Based on that if you modify the path to it may work, i haven't tried it yet. I'm not sure if the binding will work when you post the form.