【java基础】WebDataBinder

 

作用:把字符串形式的参数转换成服务端真正需要的类型的转换工具

 

那就需要借助于PropertyEditor 来帮助你完成复杂对象的对应关系,这个借口提供了两个方法,将一个property 转成string getAsText(), 另外一个方法是将string类型的值转成property对应的类型。使用起来也很简单,例子如下:

@InitBinder  
public void bindingPreparation(WebDataBinder binder) {  
  DateFormat dateFormat1 = new SimpleDateFormat("d-MM-yyyy");  
  CustomDateEditor orderDateEditor = new CustomDateEditor(dateFormat1, true);  
  DateFormat dateFormat2 = new SimpleDateFormat("MMM d, YYYY");  
  CustomDateEditor shipDateEditor = new CustomDateEditor(dateFormat2, true);  
  binder.registerCustomEditor(Date.class, "orderDate", orderDateEditor);  
  binder.registerCustomEditor(Date.class, "shipDate", shipDateEditor);  
}  

方便的完成request和property之间的binder
WebDataBinder是用来绑定请求参数到指定的属性编辑器,由于其那台传到controller里面的值是String类型的,当往model里面set这个值的时候,如果set的这个属性是个对象,Spring就会找到对应的EDITOR进行转换,然后再set进去。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

女汉纸一枚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值