Any guidance on the best way to use a Java enum as the items in a Spring <form:select> tag?
%
Any guidance on the best way to use a Java enum as the items in a Spring <form:select> tag?
%
Just had a thought: using enums for select boxes means I need some custom data binders for my validator, don't I?
Our approach is creating custom tag library that extends the Option and Options tags.
Although we use Struts tag libraries I believe the approach would be valid for Spring Tags too.
It is however a good candidate for an extension to the framework itself.
P.S. For binding you must provide a custom java.beans.PropertyEditor implementation (extends PropertyEditorSupport and override setAsText() and getAsText()) and register it.
Check the o.s.w.bind.support.WebBindingInitializer and o.s.w.bind.support.ConfigurableWebBindingInitializ er.
Last edited by Lachezar; Jan 11th, 2008 at 03:39 PM.
I registered a custom property editor with the controller managing the form. To populate the options, I had to write a method to return an array of String representations of the enum. It's an imperfect solution but probably easier than extending the tag.
Providing a converter (kind of like a PropertyEditor) is how I did it in Spring WebFlow. However, this uses the Enum name as the <option> value. I am yet to figure out how to support I18N in case the Enum name needs to be localized for different locale. Any good idea on I18N with Enum binding?
Nicer handling of Java 5 enums by the Spring MVC form taglib
https://fisheye.springsource.org/cha...ptionsTag.java