1、使用WebDataBinder进行控制器级别的注册PropertyEditor(控制器独享)
备注:转换对象必须要实现PropertyEditor接口,例如CustomDateEditor类
(1)注册ConversionService实现和自定义的类型转换器
(2)使用 ConfigurableWebBindingInitializer 注册conversionService
(3)注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter
此时可能有人会问,如果我同时使用 PropertyEditor 和 ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor 进行类型转换,如果没有找到相应的 PropertyEditor 再通过 ConversionService进行转换。
(1)实现WebBindingInitializer接口
package org.nercita.core.web.springmvc; import java.math.BigDecimal; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.core.convert.ConversionService; import org.springframework.validation.BindingErrorProcessor; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest; /** * Created by IntelliJ IDEA. * Date: * Time: 下午2:50. */ public class CustomBindInitializer implements WebBindingInitializer { private String format = "yyyy-MM-dd"; private boolean autoGrowNestedPaths = true; private boolean directFieldAccess = false; private MessageCodesResolver messageCodesResolver; private BindingErrorProcessor bindingErrorProcessor; private Validator validator; private ConversionService conversionService; private PropertyEditorRegistrar[] propertyEditorRegistrars; public void setAutoGrowNestedPaths(boolean autoGrowNestedPaths) { this.autoGrowNestedPaths = autoGrowNestedPaths; } public boolean isAutoGrowNestedPaths() { return this.autoGrowNestedPaths; } public final void setDirectFieldAccess(boolean directFieldAccess) { this.directFieldAccess = directFieldAccess; } public boolean isDirectFieldAccess() { return directFieldAccess; } public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { this.messageCodesResolver = messageCodesResolver; } public final MessageCodesResolver getMessageCodesResolver() { return this.messageCodesResolver; } public final void setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor) { this.bindingErrorProcessor = bindingErrorProcessor; } public final BindingErrorProcessor getBindingErrorProcessor() { return this.bindingErrorProcessor; } public final void setValidator(Validator validator) { this.validator = validator; } public final Validator getValidator() { return this.validator; } public final void setConversionService(ConversionService conversionService) { this.conversionService = conversionService; } public final ConversionService getConversionService() { return this.conversionService; } public final void setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar) { this.propertyEditorRegistrars = new PropertyEditorRegistrar[]{propertyEditorRegistrar}; } public final void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) { this.propertyEditorRegistrars = propertyEditorRegistrars; } public final PropertyEditorRegistrar[] getPropertyEditorRegistrars() { return this.propertyEditorRegistrars; } public void initBinder(WebDataBinder binder, WebRequest request) { binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths); SimpleDateFormat sf = new SimpleDateFormat(format); sf.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(sf, true)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); binder.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, true)); binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true)); binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true)); binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, true)); binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true)); binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); if (this.directFieldAccess) { binder.initDirectFieldAccess(); } if (this.messageCodesResolver != null) { binder.setMessageCodesResolver(this.messageCodesResolver); } if (this.bindingErrorProcessor != null) { binder.setBindingErrorProcessor(this.bindingErrorProcessor); } if ((this.validator != null) && (binder.getTarget() != null) && (this.validator.supports(binder.getTarget().getClass()))) { binder.setValidator(this.validator); } if (this.conversionService != null) { binder.setConversionService(this.conversionService); } if (this.propertyEditorRegistrars != null) for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) propertyEditorRegistrar.registerCustomEditors(binder); } public void setFormat(String format) { this.format = format; } }
(2)xml配置
<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.nercita.core.web.springmvc.StringHttpMessageConverter" /> <ref bean="msgConverter"/> </list> </property> <property name="webBindingInitializer"> <bean class="org.nercita.core.web.springmvc.CustomBindInitializer"> <property name="validator" ref="validator" /> <property name="conversionService" ref="conversionService" /> </bean> </property> </bean>