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

springmvc自动转前台传来的时间数据

2016-05-18 17:23 363 查看
Spring中默认支持的日期数据的格式是”yyyy-MM-dd”,假设现在需要支持”yyyy-MM-dd HH:mm:ss”格式的日期数据,我们就需要配置WebDataBinder以增加自定义数据绑定方式。

@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, false));
}

我现在需要将前台传来的时间戳字符串(如:1457263453000)转成Timestamp类型,刚可以按如下方法进行转换
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
@Override
public void setAsText(String value) {
setValue(new Timestamp(Long.valueOf(value)));
}
});

}
只需要将上述方法定义成一个父类,在需要的controller中继承一下即可,本人是用springmvc4来实现的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息