Jörg Heinicke
Mar 5th, 2007, 06:21 AM
Up to now I saw repeatedly questions about non-working <form:select> (ie. using Spring's form taglib) when using custom property editors. Such questions mostly stayed unanswered. Now I also have such a case - and it neither works for me. But one after another ...
I have the following select in my JSP:
<form:select path="company">
<form:option value="" label="all"/>
<form:options items="${companies}" itemValue="id" itemLabel="name"/>
</form:select>
And I have also registered successfully my custom property editor for converting Company objects into string and back. I see this one coming into play when doing remote debugging.
Now the problem: If I select a company and submit the form, the company gets selected, but on redisplay of the form it is no longer selected. Remote debugging shows that Spring behaves correct up to the point where the tags are written out. The final method not working as expected seems to be org.springframework.web.servlet.tags.form. SelectedValueComparator#exhaustiveCompare:
Ignoring LabeledEnum the first comparison made is
ObjectUtils.getDisplayString(boundValue).equals(ca ndidateDisplayString)
where boundValue is a Company object and candidateDisplayString is
ObjectUtils.getDisplayString(candidate) candidate is actually the @itemValue from the code in the JSP using the taglib, in this case the id property of Company is of type Integer.
ObjectUtils.getDisplayString() uses toString() implementation of the class, so at the end Company.toString() (providing no implementation, so defaulting to Object.toString()) is compared to Integer.toString(), which fails expectedly.
The last comparison is only done when candidate is of type String
if (propertyEditor != null && candidate instanceof String)
and here I wonder if that condition is correct. From what I understand the comparison between the bound value (ie. the selected Company object) and the candidate value (ie. one of the Company objects from the collection) must be made on the same type of the objects, so either:
- Company objects directly, not Company on the one side, its itemValue on the other side
- their itemValue properties on both sides
- the string representations of the Company objects on both sides
- or the string representations of their itemValues on both sides.
The latter two would be tried if there would not be that strange condition mentioned above (candidate being of type String).
So has anybody <form:select> with Spring's taglib and custom property editor working and can prove my investigations from above being wrong? If not, can anybody reproduce my investigations and confirm it's somehow a bug?
Thanks in advance
Jörg
I have the following select in my JSP:
<form:select path="company">
<form:option value="" label="all"/>
<form:options items="${companies}" itemValue="id" itemLabel="name"/>
</form:select>
And I have also registered successfully my custom property editor for converting Company objects into string and back. I see this one coming into play when doing remote debugging.
Now the problem: If I select a company and submit the form, the company gets selected, but on redisplay of the form it is no longer selected. Remote debugging shows that Spring behaves correct up to the point where the tags are written out. The final method not working as expected seems to be org.springframework.web.servlet.tags.form. SelectedValueComparator#exhaustiveCompare:
Ignoring LabeledEnum the first comparison made is
ObjectUtils.getDisplayString(boundValue).equals(ca ndidateDisplayString)
where boundValue is a Company object and candidateDisplayString is
ObjectUtils.getDisplayString(candidate) candidate is actually the @itemValue from the code in the JSP using the taglib, in this case the id property of Company is of type Integer.
ObjectUtils.getDisplayString() uses toString() implementation of the class, so at the end Company.toString() (providing no implementation, so defaulting to Object.toString()) is compared to Integer.toString(), which fails expectedly.
The last comparison is only done when candidate is of type String
if (propertyEditor != null && candidate instanceof String)
and here I wonder if that condition is correct. From what I understand the comparison between the bound value (ie. the selected Company object) and the candidate value (ie. one of the Company objects from the collection) must be made on the same type of the objects, so either:
- Company objects directly, not Company on the one side, its itemValue on the other side
- their itemValue properties on both sides
- the string representations of the Company objects on both sides
- or the string representations of their itemValues on both sides.
The latter two would be tried if there would not be that strange condition mentioned above (candidate being of type String).
So has anybody <form:select> with Spring's taglib and custom property editor working and can prove my investigations from above being wrong? If not, can anybody reproduce my investigations and confirm it's somehow a bug?
Thanks in advance
Jörg