提交 d6bab3b6 编写于 作者: J Juergen Hoeller

turned FormattingPropertyEditorAdapter into ConvertingPropertyEditorAdapter

上级 f8dd5fb5
......@@ -24,7 +24,7 @@ import org.springframework.beans.PropertyAccessorUtils;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.format.support.FormattingPropertyEditorAdapter;
import org.springframework.core.convert.support.ConvertingPropertyEditorAdapter;
import org.springframework.util.Assert;
/**
......@@ -151,7 +151,7 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul
TypeDescriptor td = (field != null ?
getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) :
TypeDescriptor.valueOf(valueType));
editor = new FormattingPropertyEditorAdapter(this.conversionService, valueType);
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
}
return editor;
}
......
......@@ -22,6 +22,7 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.ConfigurablePropertyAccessor;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessException;
......@@ -459,17 +460,25 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
return this.validator;
}
//---------------------------------------------------------------------
// Implementation of PropertyEditorRegistry/TypeConverter interface
//---------------------------------------------------------------------
/**
* Set the ConversionService to use for field value formatting in preference to JavaBeans PropertyEditors.
* @since 3.0
* Specify a Spring 3.0 ConversionService to use for converting
* property values, as an alternative to JavaBeans PropertyEditors.
*/
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
//---------------------------------------------------------------------
// Implementation of PropertyEditorRegistry/TypeConverter interface
//---------------------------------------------------------------------
/**
* Return the associated ConversionService, if any.
*/
public ConversionService getConversionService() {
return this.conversionService;
}
@SuppressWarnings("unchecked")
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
......@@ -492,6 +501,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
public <T> T convertIfNecessary(
Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
return getTypeConverter().convertIfNecessary(value, requiredType, methodParam);
}
......
......@@ -14,46 +14,53 @@
* limitations under the License.
*/
package org.springframework.format.support;
package org.springframework.core.convert.support;
import java.beans.PropertyEditorSupport;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.Assert;
/**
* Adapter that exposes a {@link java.beans.PropertyEditor} for any given
* {@link org.springframework.format.Formatter}, retrieving the current
* Locale from {@link org.springframework.context.i18n.LocaleContextHolder}.
* {@link org.springframework.core.convert.ConversionService} and specific target type.
*
* @author Juergen Hoeller
* @since 3.0
*/
public class FormattingPropertyEditorAdapter extends PropertyEditorSupport {
public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
private final ConversionService conversionService;
private final Class<?> fieldType;
private final TypeDescriptor targetDescriptor;
/**
* Create a new FormattingPropertyEditorAdapter for the given Formatter.
* @param formatter the Formatter to wrap
* Create a new ConvertingPropertyEditorAdapter for a given
* {@link org.springframework.core.convert.ConversionService}
* and the given target type.
* @param conversionService the ConversionService to delegate to
* @param targetDescriptor the target type to convert to
*/
public FormattingPropertyEditorAdapter(ConversionService formattingService, Class<?> fieldType) {
Assert.notNull(formattingService, "ConversionService must not be null");
Assert.notNull(formattingService, "FieldType must not be null");
this.conversionService = formattingService;
this.fieldType = fieldType;
public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
Assert.notNull(conversionService, "ConversionService must not be null");
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
this.conversionService = conversionService;
this.targetDescriptor = targetDescriptor;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(this.conversionService.convert(text, this.fieldType));
setValue(this.conversionService.convert(text, stringDescriptor, this.targetDescriptor));
}
@Override
public String getAsText() {
return this.conversionService.convert(getValue(), String.class);
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, stringDescriptor);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册