SSM中日期问题(前端,后台)

后台接受前端的日期:

前端使用input date类型向后台传递日期类型, 传到后台是字符串类型的,如果直接赋值给date类型,就会报400类型不匹配错误

可以在日期赋值给日期类型的值之前将其转换为date类型.

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

将以上代码加到需要进行日期转换的controller中,进行初始化绑定, 前台的日期首先会到这里被转换为日期类型的值,在赋值的时候就不会出现参数不匹配

数据库的日期值为null,取出来报错的解决方案

jdbc.url=jdbc:mysql:///ht?zeroDateTimeBehavior=convertToNull

在连接数据库的时候制定这个参数就可以解决

前端的日期插件

1.html5的input框

<input type="date">

2.my97 datepicker

<input type="text" onclick="WdatePicker({el:this,isShowOthers:true,dateFmt:'yyyy-MM-dd'});"/>

猜你喜欢

转载自blog.csdn.net/ifenggege/article/details/82808079