V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  admin9527  ›  全部回复第 1 页 / 共 1 页
回复总数  2
2017-04-06 09:44:29 +08:00
回复了 wly19960911 创建的主题 Java 关于 spring MVC 的 @RequestBody 接收 json, Date 类无法转换的问题
@cs4814751 恩,是的,没注意,原来都是用在 ajax 表单提交上的
2017-04-06 09:37:00 +08:00
回复了 wly19960911 创建的主题 Java 关于 spring MVC 的 @RequestBody 接收 json, Date 类无法转换的问题
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);

SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
datetimeFormat.setLenient(false);

binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat, true));
binder.registerCustomEditor(java.sql.Timestamp.class,new CustomDateEditor(datetimeFormat, true));
}
可以在控制 controller 上加注解,注入的时候应该就会自动转了,不过想想直接用 string 接收方便多了。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2691 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 05:14 · PVG 13:14 · LAX 22:14 · JFK 01:14
Developed with CodeLauncher
♥ Do have faith in what you're doing.