springMvc中的@InitBinder
发布日期:2021-06-29 01:16:29 浏览次数:2 分类:技术文章

本文共 566 字,大约阅读时间需要 1 分钟。

这个主要是讲解一下自己遇到的@InitBinder的问题,这个@InitBinder实际上就是在springmvc中,当表单向后台传递日期类型的数据时,由于springmvc不能自动在后台转化Date类型的参数(int可以自动转化成String等等),所以用到了@InitBinder。实际上客户端向服务器传递参数都是通过request.getparameter来传递的,传递到后台实际上是字符串,这时我们就需要一个转化工具WebDataBinder,将字符串转化成我们所需要的类型。

我们不必自己创建WebDataBinder,我们只需要想他注册参数类型对应的属性编辑器就可以了PropertyEditor。
具体代码如下
` @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
dateFormat.setLenient(false); //禁止自动转化日期
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

} `

转载地址:https://blog.csdn.net/ys3198/article/details/53070064 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:springMvc的点点滴滴
下一篇:guava提升你的开发效率

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月15日 21时38分58秒