指点成金-最美分享吧

登录

spring-web中的WebDataBinder理解

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了spring-web中的WebDataBinder理解相关的知识,希望对你有一定的参考价值。

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

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

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

 

以上是关于spring-web中的WebDataBinder理解的主要内容,如果未能解决你的问题,请参考以下文章