spring-web中的WebDataBinder理解

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 CustomDateEditor(sdf,true));//true表示允许空值,false不允许
}

 

posted @ 2018-05-27 11:33  夏威夷8080  阅读(608)  评论(0编辑  收藏  举报