如果想使用全局范围的自定义编辑器,可以使用WebBindingInitializer接口,并在该类中注册自定义编译器。

public class DateBindingInitializer implements WebBindingInitializer {
	@Override
	public void initBinder(WebDataBinder binder, WebRequest request) {
		// 注册自定义编辑器
		binder.registerCustomEditor(Date.class, new DateEditor());
	}
}

DateEditor在上篇blog中已定义。这时就不需要在Controller中使用@InitBinder。但是需要在springmvc-config.xml中配置

<!-- 通过AnnotationMethodHandlerAdapter装配自定义编辑器 -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.fkjava.binding.DateBindingInitializer" />
    </property>
</bean>

如果你在spring中使用了3中数据转换方法,那么他们的优先顺序

  • 1,通过@InitBinder装配的自定义编辑器
  • 2,通过ConversionService装配的自定义转换器
  • 3,通过WebBindingInitializer接口装配的全局自定义编辑器。

写完这3片blog后,其实我更好奇是其后面隐藏的spring使用数据转换器的流程,这个在稍后的blog中介绍。

相关文章:

  • 2021-06-02
  • 2021-11-28
  • 2021-03-31
  • 2021-12-09
  • 2022-01-03
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2021-10-30
  • 2022-01-16
  • 2021-06-04
  • 2021-11-11
  • 2022-02-08
  • 2022-02-18
相关资源
相似解决方案