refined PropertyEditor exposure for the ConversionService case

This commit is contained in:
Juergen Hoeller 2009-12-15 11:34:19 +00:00
parent 0057481a0a
commit 5f9f69958e
3 changed files with 21 additions and 14 deletions

View File

@ -117,9 +117,8 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
if (this.conversionService != null) { if (this.conversionService != null) {
// Try custom formatter... // Try custom formatter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField); TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
TypeDescriptor stringDesc = TypeDescriptor.valueOf(String.class); if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, TypeDescriptor.STRING)) {
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, stringDesc)) { return this.conversionService.convert(value, fieldDesc, TypeDescriptor.STRING);
return this.conversionService.convert(value, fieldDesc, stringDesc);
} }
} }
return value; return value;
@ -153,8 +152,10 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
TypeDescriptor td = (field != null ? TypeDescriptor td = (field != null ?
getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) : getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) :
TypeDescriptor.valueOf(valueType)); TypeDescriptor.valueOf(valueType));
if (this.conversionService.canConvert(TypeDescriptor.STRING, td)) {
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td); editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
} }
}
return editor; return editor;
} }

View File

@ -31,12 +31,12 @@ import org.springframework.util.Assert;
*/ */
public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport { public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
private final ConversionService conversionService; private final ConversionService conversionService;
private final TypeDescriptor targetDescriptor; private final TypeDescriptor targetDescriptor;
private final boolean canConvertToString;
/** /**
* Create a new ConvertingPropertyEditorAdapter for a given * Create a new ConvertingPropertyEditorAdapter for a given
@ -50,17 +50,23 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null"); Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
this.conversionService = conversionService; this.conversionService = conversionService;
this.targetDescriptor = targetDescriptor; this.targetDescriptor = targetDescriptor;
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.STRING);
} }
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
setValue(this.conversionService.convert(text, stringDescriptor, this.targetDescriptor)); setValue(this.conversionService.convert(text, TypeDescriptor.STRING, this.targetDescriptor));
} }
@Override @Override
public String getAsText() { public String getAsText() {
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, stringDescriptor); if (this.canConvertToString) {
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.STRING);
}
else {
return null;
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,16 +59,16 @@ abstract class ValueFormatter {
if (propertyEditor != null && !(value instanceof String)) { if (propertyEditor != null && !(value instanceof String)) {
try { try {
propertyEditor.setValue(value); propertyEditor.setValue(value);
return getDisplayString(propertyEditor.getAsText(), htmlEscape); String text = propertyEditor.getAsText();
if (text != null) {
return getDisplayString(text, htmlEscape);
}
} }
catch (Throwable ex) { catch (Throwable ex) {
// The PropertyEditor might not support this value... pass through. // The PropertyEditor might not support this value... pass through.
}
}
return getDisplayString(value, htmlEscape); return getDisplayString(value, htmlEscape);
} }
}
else {
return getDisplayString(value, htmlEscape);
}
}
} }