Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataBinding error with Java 5 enums [SPR-3387] #8070

Closed
spring-issuemaster opened this issue Apr 18, 2007 · 1 comment

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Apr 18, 2007

William Shields opened SPR-3387 and commented

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.


Affects: 2.0.4

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 25, 2007

Juergen Hoeller commented

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.