SpringMVC配置全局日期转换器,处理日期转换异常

本文介绍了如何在Spring MVC 3.1.1中通过实现WebBingingInitializer接口的日期转换类来实现自定义日期转换。包括配置步骤、原理解析以及解决冲突的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring3.0配置日期转换可以通过配置自定义实现WebBingingInitializer接口的一个日期转换类来实现,方法如下

转换类:

  1. publicclassDateConverterimplementsWebBindingInitializer{
  2. publicvoidinitBinder(WebDataBinderbinder,WebRequestrequest){
  3. SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-dd");
  4. binder.registerCustomEditor(Date.class,newCustomDateEditor(df,false));
  5. }
  6. }

在spring-servlet.xml当中的进行注册:

  1. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  2. <!--日期格式转换-->
  3. <propertyname="webBindingInitializer">
  4. <beanclass="DateConverter"/>
  5. </property>
  6. </bean>

spring3.1.1的处理进行调整,所以按照3.0的写法在3.1.1里面是无效的,通过查找资料及测试,发现可行方法

原因:

annotation-driven缺省注册类的改变

Spring 3.0.x中使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 来注册handler method和request的mapping关系。AnnotationMethodHandlerAdapter来在实际调用handlermethod前对其参数进行处理。

在spring mvc 3.1中,对应变更为
DefaultAnnotationHandlerMapping -> RequestMappingHandlerMapping
AnnotationMethodHandlerAdapter -> RequestMappingHandlerAdapter
AnnotationMethodHandlerExceptionResolver -> ExceptionHandlerExceptionResolver

以上都在使用了annotation-driven后自动注册。
而且对应分别提供了AbstractHandlerMethodMapping , AbstractHandlerMethodAdapter和 AbstractHandlerMethodExceptionResolver以便于让用户更方便的实现自定义的实现类。

<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

springmvc<mvc:annotation-driven />会自动启动Spring MVC的注解功能,但实际它做了哪些工作呢?

Java代码
  1. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  2. <propertyname="order"value="1"/>
  3. </bean>
  4. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  5. <propertyname="webBindingInitializer">
  6. <beanclass="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
  7. <propertyname="conversionService"ref="conversionService"/>
  8. <propertyname="validator"ref="validator"/>
  9. </bean>
  10. </property>
  11. </bean>
  12. <beanid="conversionService"class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory"/>
  13. <beanid="validator"class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>


从上面的配置可以看出,我的配置应该是被sping的配置覆盖了,<mvc:annotation-driven />配置中已经包含了webBindingInitializer的配置,看来使用<mvc:annotation-driven />后与原来的配置出现了重复,这种情况下不管<mvc:annotation-driven />放在上面还是放在下面都会出现问题。

解决方法:

使用conversion-service来注册自定义的converter
DataBinder实现了PropertyEditorRegistry, TypeConverter这两个interface,而在spring mvc实际处理时,返回值都是return binder.convertIfNecessary(见HandlerMethodInvoker中的具体处理逻辑)。因此可以使用customer conversionService来实现自定义的类型转换。

<mvc:annotation-driven />中配置可以看出,AnnotationMethodHandlerAdapter已经配置了webBindingInitializer,我们可以通过设置其属性conversionService来实现自定义类型转换。

Java代码
  1. <beanid="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  2. <propertyname="converters">
  3. <list>
  4. <beanclass="com.doje.XXX.web.DateConverter"/>
  5. </list>
  6. </property>
  7. </bean>

需要修改spring service context xml配置文件中的annotation-driven,增加属性conversion-service指向新增的conversionService bean。

Java代码
  1. <mvc:annotation-drivenconversion-service="conversionService"/>

实际自定义的converter如下。

Java代码
  1. publicclassDateConverterimplementsConverter<String,Date>{
  2. @Override
  3. publicDateconvert(Stringsource){
  4. SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-dd");
  5. dateFormat.setLenient(false);
  6. try{
  7. returndateFormat.parse(source);
  8. }catch(ParseExceptione){
  9. e.printStackTrace();
  10. }
  11. returnnull;
  12. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值