轉載自: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類型的數據也有很多的話,這樣每一個都配置很麻煩。