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不允许
}
原文地址:https://www.cnblogs.com/shamo89/p/9095508.html