Announcement Announcement Module
Collapse
No announcement yet.
<form:select> and Java enum Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • <form:select> and Java enum

    Any guidance on the best way to use a Java enum as the items in a Spring <form:select> tag?

    %

  • #2
    Just had a thought: using enums for select boxes means I need some custom data binders for my validator, don't I?

    Comment


    • #3
      Originally posted by duffymo View Post
      Any guidance on the best way to use a Java enum as the items in a Spring <form:select> tag?

      %
      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, 03:39 PM.

      Comment


      • #4
        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.

        Comment


        • #5
          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?

          Comment


          • #6
            Nicer handling of Java 5 enums by the Spring MVC form taglib

            https://fisheye.springsource.org/cha...ptionsTag.java

            Comment

            Working...
            X