spring-web中的WebDataBinder理解详解编程语言

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

[email protected](初始化绑定封装)注解和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://blog.ytso.com/15725.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论