您的位置:首页 > 编程语言 > Java开发

4000 springMVC中日期格式转换问题

2016-10-21 16:46 441 查看
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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: