turned FormattingPropertyEditorAdapter into ConvertingPropertyEditorAdapter
This commit is contained in:
parent
f8dd5fb5dc
commit
d6bab3b674
|
|
@ -1,59 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.format.support;
|
|
||||||
|
|
||||||
import java.beans.PropertyEditorSupport;
|
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionService;
|
|
||||||
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}.
|
|
||||||
*
|
|
||||||
* @author Juergen Hoeller
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
public class FormattingPropertyEditorAdapter extends PropertyEditorSupport {
|
|
||||||
|
|
||||||
private final ConversionService conversionService;
|
|
||||||
|
|
||||||
private final Class<?> fieldType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new FormattingPropertyEditorAdapter for the given Formatter.
|
|
||||||
* @param formatter the Formatter to wrap
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setAsText(String text) throws IllegalArgumentException {
|
|
||||||
setValue(this.conversionService.convert(text, this.fieldType));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAsText() {
|
|
||||||
return this.conversionService.convert(getValue(), String.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -24,7 +24,7 @@ import org.springframework.beans.PropertyAccessorUtils;
|
||||||
import org.springframework.beans.PropertyEditorRegistry;
|
import org.springframework.beans.PropertyEditorRegistry;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.format.support.FormattingPropertyEditorAdapter;
|
import org.springframework.core.convert.support.ConvertingPropertyEditorAdapter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -151,7 +151,7 @@ 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));
|
||||||
editor = new FormattingPropertyEditorAdapter(this.conversionService, valueType);
|
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
|
||||||
}
|
}
|
||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.beans.ConfigurablePropertyAccessor;
|
import org.springframework.beans.ConfigurablePropertyAccessor;
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
import org.springframework.beans.PropertyAccessException;
|
import org.springframework.beans.PropertyAccessException;
|
||||||
|
|
@ -459,17 +460,25 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||||
return this.validator;
|
return this.validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Implementation of PropertyEditorRegistry/TypeConverter interface
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the ConversionService to use for field value formatting in preference to JavaBeans PropertyEditors.
|
* Specify a Spring 3.0 ConversionService to use for converting
|
||||||
* @since 3.0
|
* property values, as an alternative to JavaBeans PropertyEditors.
|
||||||
*/
|
*/
|
||||||
public void setConversionService(ConversionService conversionService) {
|
public void setConversionService(ConversionService conversionService) {
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
/**
|
||||||
// Implementation of PropertyEditorRegistry/TypeConverter interface
|
* Return the associated ConversionService, if any.
|
||||||
//---------------------------------------------------------------------
|
*/
|
||||||
|
public ConversionService getConversionService() {
|
||||||
|
return this.conversionService;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
|
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
|
||||||
|
|
@ -492,6 +501,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
||||||
|
|
||||||
public <T> T convertIfNecessary(
|
public <T> T convertIfNecessary(
|
||||||
Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
|
Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
|
||||||
|
|
||||||
return getTypeConverter().convertIfNecessary(value, requiredType, methodParam);
|
return getTypeConverter().convertIfNecessary(value, requiredType, methodParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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.core.convert.ConversionService} and specific target type.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
|
||||||
|
|
||||||
|
private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
|
||||||
|
|
||||||
|
private final ConversionService conversionService;
|
||||||
|
|
||||||
|
private final TypeDescriptor targetDescriptor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 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, stringDescriptor, this.targetDescriptor));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAsText() {
|
||||||
|
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, stringDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue