1、注解方式,在controller层通过initBinder注解实现
@InitBinder
public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws Exception {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
binder.registerCustomEditor(Date.class, dateEditor);
}
2、类型转换,SpringMvc提供了Converter接口
public class DateConvert implements Converter{ @Override public Date convert(String stringDate) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(stringDate); } catch (ParseException e) { e.printStackTrace(); } return null; } }
spring.xml中配置转换器
以上这篇SpringMVC 传日期参数到后台的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



