spring mvc 日期类型转换问题 @initBinder解决前台与后台的日期格式不统一问题

在用ssm框架开发web程序的时候发现后台传到前台的日期可能格式不是我们想要的
可以用以下解决方案
在controller里面添加一个方法
/**
* 加入以下方法 解决spring无法转换date数据的问题
* @param request
* @param binder
*/
@InitBinder
public void InitBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
// 不要删除下行注释!!! 将来"yyyy-MM-dd"将配置到properties文件中
// SimpleDateFormat dateFormat = new
// SimpleDateFormat(getText("date.format", request.getLocale()));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
dateFormat, true));
}

这里如果项目中经常涉及这种格式转换,可以将这个方法放在baseController里面,用继承就可以了,避免每一个controller里面都要去写一个这个方法。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值