例子:将一个实体类从前台保存至后台,form表单有个时间对象,库里面存的字段为date类型,为何要在Controller层,要加这么一个方法:
/**
* 转换时间格式
* @param binder
*/
protected void BinderDate(ServletRequestDataBinder binder) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
问题来了:
1、为什么不加会报错?
2、加了之后为什么不报错了?
请尽量以简单的语言让我明白,感谢!!
------解决思路----------------------
通过form表单穿过来的参数都是string型的,所以要先转成date型,才能入库,因为你数据库的日期是date型