diff --git a/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java b/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java index 091fead14efc8c60de0e93bf79622db713b488c6..1b3c0e44ce89821ce56029bc9f536fe80239c834 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java @@ -117,9 +117,8 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul if (this.conversionService != null) { // Try custom formatter... TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField); - TypeDescriptor stringDesc = TypeDescriptor.valueOf(String.class); - if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, stringDesc)) { - return this.conversionService.convert(value, fieldDesc, stringDesc); + if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, TypeDescriptor.STRING)) { + return this.conversionService.convert(value, fieldDesc, TypeDescriptor.STRING); } } return value; @@ -153,7 +152,9 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul TypeDescriptor td = (field != null ? getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) : TypeDescriptor.valueOf(valueType)); - editor = new ConvertingPropertyEditorAdapter(this.conversionService, td); + if (this.conversionService.canConvert(TypeDescriptor.STRING, td)) { + editor = new ConvertingPropertyEditorAdapter(this.conversionService, td); + } } return editor; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java index 066d2666be87258770f11a489db4175b2324919d..d78630c9afd216bfb86cf13f64435f0371a26483 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java @@ -31,12 +31,12 @@ import org.springframework.util.Assert; */ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport { - private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class); - private final ConversionService conversionService; private final TypeDescriptor targetDescriptor; + private final boolean canConvertToString; + /** * Create a new ConvertingPropertyEditorAdapter for a given @@ -50,17 +50,23 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport { Assert.notNull(targetDescriptor, "TypeDescriptor must not be null"); this.conversionService = conversionService; this.targetDescriptor = targetDescriptor; + this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.STRING); } @Override 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 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; + } } } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ValueFormatter.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ValueFormatter.java index 66b5ed7a58f9617b35e52c6c0deb03b142d2fd16..b3c23fea70d39b84b61858f08d2c1cbd42ed648a 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ValueFormatter.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ValueFormatter.java @@ -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"); * 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)) { try { propertyEditor.setValue(value); - return getDisplayString(propertyEditor.getAsText(), htmlEscape); + String text = propertyEditor.getAsText(); + if (text != null) { + return getDisplayString(text, htmlEscape); + } } catch (Throwable ex) { // The PropertyEditor might not support this value... pass through. - return getDisplayString(value, htmlEscape); } } - else { - return getDisplayString(value, htmlEscape); - } + return getDisplayString(value, htmlEscape); } }