Results 1 to 6 of 6

Thread: <form:select> and Java enum

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    208

    Default <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. #2
    Join Date
    May 2005
    Posts
    208

    Default

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

  3. #3
    Join Date
    Nov 2004
    Location
    Bulgaria
    Posts
    35

    Lightbulb

    Quote 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 at 03:39 PM.

  4. #4
    Join Date
    Jun 2008
    Posts
    3

    Default

    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.

  5. #5
    Join Date
    Sep 2008
    Posts
    12

    Default

    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?

  6. #6

    Default

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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •