当把前台对象传递到后台时,使用该方法
public ModelAndView authCompanySave(HttpServletRequest request, HttpServletResponse response,Users users){....}
如果users对象中有日期格式的字段,则会报错,需要加上下面的内容。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值,false:不能为空值
}
本文介绍了一种在将包含日期格式字段的对象从前台传递到后台时避免报错的方法。通过在Controller中使用@InitBinder注解注册CustomDateEditor,可以确保日期格式正确解析。
822

被折叠的 条评论
为什么被折叠?



