码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中的WebDataBinder浅析

时间:2016-08-18 14:33:20      阅读:842      评论:0      收藏:0      [点我收藏+]

标签:

Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等)。如果传递过来的是特殊对象,则需要手动进行封装。

Spring提供了@initBinder(初始化绑定封装)注解和WebDataBinder工具。用户只需要向WebDataBinder注册自己需要的类型的属性编辑器即可。

/*
前台传递过来的String类型时间,通过下面的初始化绑定,转换成Date类型
*/
@initBinder
public void initBinder(WebDataBinder binder){
  SimpleDateFormate sdf=new SimpleDateFormate("yyyy-MM-dd HH:mm");  
  binder.registerCustomEditor(Date.class,new CustomEditor(sdf,true));//true表示允许空值,false不允许
}

更多详细内容转载链接

1,http://blog.csdn.net/hongxingxiaonan/article/details/50282001(前台传递过来非Date类型,及WebDataBinder的其它方法)

2,http://www.cnblogs.com/AloneSword/p/3998943.html(前台传递过来两个不同名和不同格式的Date类型)

Spring中的WebDataBinder浅析

标签:

原文地址:http://www.cnblogs.com/aquriushu/p/5783691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!