SpringMVC中不能自动将Spring转换成Date,所以要在Action实现类中添加日期转换器,用来转换日期格式,不然,jsp页面数据传输时会不能和javaBean实体类的属性相对应。在Action实现类中的构造方法后面添加initBinder方法
/**
* springMVC不能自动将String类型转换为Date类型
* 自定义类型转换器,将String-》Date类型(格式yyyy-MM-dd)
*/
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
//向springMVC内部注入一个自定义的类型转换器
//参数一:将String转成什么类型的字节码
//参数二:自定义转换规则
//true表示可以为空
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
本文介绍如何在SpringMVC框架中解决无法自动将字符串转换为日期的问题。通过自定义类型转换器,确保从JSP页面传来的数据能够正确地与JavaBean实体类中的日期属性匹配。
551

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



