Spring mvc Date类型参数为空时,controller报错

在浏览器端传过来的日期参数的值为空时,会报出如下错误:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'birthday': rejected value []; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value ''; nested exception is java.lang.IllegalArgumentException]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)

分析:
在spring mvc对浏览器端传送过来的参数进行解析的时候,主要分为两中选择。
1 按照相关类型,获取PropertyEditorSupport 对象进行解析。
2 如果出现在1中开发人员没有配置相关的PropertyEditorSupport 进行解析,则进入到spring bean的核心,进行类型转换。使用org.springframework.core.convert.support.ObjectToObjectConverter 进行转换。详见这个类中的public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) 方法,source就是传入的参数值,sourceType原目标类型,在进行Date类型转换的过程中,是java.lang.String类型,targetType目标类型是java.util.Date。在浏览器端没有发过来任何参数的时候,source是""属于java.lang.String。通过代码可以找到3.1.1 release版本是66行。代码如下:
return constructor.newInstance(source);
走这段代码,这个构造器是:Date(String)。从new Date("")中可以看到,会抛出异常,为此大家在spring mvc中,会抛出异常。

解决办法:
基于以上,想在2中解决,实在不太可能。只能在第一步动手。
按照1中的解决办法,有两种手段解决:
1.1 按照大部分网上的做法。代码如下:
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
// 注册自定义的属性编辑器
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
CustomDateEditor dateEditor = new CustomDateEditor(df, true);
// 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换
binder.registerCustomEditor(Date.class, dateEditor);
}
这样的做法,会出现一种情况,那就是只能解析("yyyy/MM/dd HH:mm:ss")这种格式的时间格式,会受时间格式的显示。
1.2 继承PropertyEditorSupport类,覆盖public void setAsText(String text) throws IllegalArgumentException 方法。代码如下:
package com.pandawork.core.web.spring.type;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import org.springframework.util.StringUtils;
/**
*
* @author Lionel
* @e-mainl lionel@pandawork.net
* @Time 2012-9-11
*
*/
public class MVCDateFormat extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (!StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
} else {
setValue(new Date(text));
}
}
}
然后把这个类直接注册到web数据绑定中,代码如下:
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
// 注册日期更新
binder.registerCustomEditor(Date.class, new MVCDateFormat());
}
这样解决的不好的地方是new Date(String)已经过期,不过还能用。但是走spring的核心,也是获取到同样的构造器来实现。感觉不用担心这个地方。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值