Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-3388

<form:*> tag library assumes the value of a Java 5 enum property is the value of toString()

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.4
    • Fix Version/s: 2.0.5
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      In XML Spring config files, you can use name() of an enum value when setting the property, for example:

      <bean id="person1">
      <property name="gender" value="MALE"/>
      ...
      </bean>

      for

      public enum Gender

      { MALE, FEMALE }

      ;

      It does this lookup based on name().

      The Spring MVC form tag library is different and uses toString(). A similar issue was raised in Spring Webflow and fixed in the latest 1.0.2 release. This should also be fixed in Spring MVC. The default implementation of a Java 5 enum is effectively:

      public String toString()

      { return name(); }

      however it is reasonable and common to override this. For example:

      public Gender {
      MALE("Male"),
      FEMALE("Female");

      private String description;

      Gender(String description)

      { this.description = description; }

      public String toString()

      { return description; }

      }

      Why do this? Because the name can be considered the value whereas the toString() can be viewed as the description the user sees and selects, which brings up the issue that there really isn't a nice way to do this.

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              wshields William Shields
              Last updater:
              Spring Issuemaster
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                26 weeks, 2 days ago