转载自:https://blog.csdn.net/preferG/article/details/78090669
转载自:http://cfanz.cn/index.php?c=article&a=read&id=303559
一、问题
springMVC有一个比较奇葩的问题,就是如果接受参数是日期(java.util.Date)类型或者参数是包含了(java.util.Date)得POJO将会导致无法进入Controller的方法。没想明白为什么spring作为那么成熟的框架没有兼容这个问题。好了,不废话了,下面讲一下解决办法(不一定是最佳方案,仅供参考)
在网上查找是因为 springMVC将字符串直接认为是Date类型了,可是如果空字符串的话,就不认识了
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//可以为空
}
可是这样的话只能在当前的controller中起到作用, 但我们的controller存在很多并且实体类中的date类型的数据也有很多的话,这样每一个都配置很麻烦。