Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major 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:
      false

      Description

      BeanWrapperImpl uses TypeConverterDelegate. This class is not open to extension and not easily pluggable in a Spring MVC or Webflow environment. That aside, it has what I consider to be a bug. In convertIfNecessary() you have the following fallback code block for Java 5 enums:

      try

      { Field enumField = requiredType.getField((String) convertedValue); convertedValue = enumField.get(null); }

      catch (Throwable ex) {
      if (logger.isTraceEnabled())

      { logger.trace("Field [" + convertedValue + "] isn't an enum value", ex); }
      }

      IMHO this should be changed to:

      try {
      if (StringUtils.hasText((String)convertedValue) { Field enumField = requiredType.getField((String) convertedValue); convertedValue = enumField.get(null); } else { convertedValue = null; }
      }
      catch (Throwable ex) {
      if (logger.isTraceEnabled()) { logger.trace("Field [" + convertedValue + "] isn't an enum value", ex); }

      }

      This is to circumvent a problem where you can't set null values for Java 5 enum properties, which is particularly a problem for Web pages. Consider the example:

      <form:select path="gender">
      <form:option value="" label="Select One"/>
      <form:option value="MALE" label="Male"/>
      <form:option value="FEMALE" label="Female"/>
      </form:select>

      assuming:

      public enum Gender

      { MALE, FEMALE; }

      and the gender property being of type Gender.

      This forces you to:

      • use a custom property editor;
      • use an empty dummy value (like a Gender instance of NOT_SELECTED); or
      • not use Java 5 enums.

      Not selecting an option (translating to a null value) is a valid and common scenario. A custom property editor shouldn't be required for this.

        Activity

        Hide
        Juergen Hoeller added a comment -

        Good point - I've changed this accordingly. However, we only convert an empty String ("") to null in case of a enum target type now, not a String that contains whitespace characters, since we strictly match the String for actual enum value names as well.

        This fix should be available in the next 2.0.5 snapshot (http://www.springframework.org/snapshots), along with the enum handling change in the form tag library. Please give it a try and let us know whether it works for you!

        Juergen

        Show
        Juergen Hoeller added a comment - Good point - I've changed this accordingly. However, we only convert an empty String ("") to null in case of a enum target type now, not a String that contains whitespace characters, since we strictly match the String for actual enum value names as well. This fix should be available in the next 2.0.5 snapshot ( http://www.springframework.org/snapshots ), along with the enum handling change in the form tag library. Please give it a try and let us know whether it works for you! Juergen

          People

          • Assignee:
            Juergen Hoeller
            Reporter:
            William Shields
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              7 years, 1 week, 1 day ago