SpringMVC标签数据绑定Command类的使用详解

一、SpringMVC的标签实现数据绑定详解

        在学习BaseCommandController时,我们知道,当提交表单时,controller会把表单元素注入到Command类里,但是系统注入的只能是基本类型,如int,char,String。但当我们在Command类里需要复杂类型,如Integer或自己定义的类时,controller就不会那么聪明了。
1、一般的做法是在自己的controller里重写(override)initBinder()方法。这里我们修改一下学习SimpleFormController的那个例子。

覆盖initBinder()方法

2、在执行请求参数到Command对象绑定之前,初始化一个可用的DataBinder实例,就是有了一个ServletRequestDataBinder实例之后,我们可以添加自定义的PropertyEdtior,以支持某些特殊数据类型的数据的绑定,或者排除某些不想绑定的请求参数,这些定制行为可以通过覆写initBinder()方法引入。

3、下面的例子是在之前的SimpleFormController基础上进行了一些小小的变动。

Command类中的主要变动时增加了一个Date类型的birthday字段,get字段的类型由int变成了Integer,这些都是不能直接将请求参数绑定到Command对象上。

 

[java] view plain copy  print?

  1. public class UserModel {  
  2.     private String account;  
  3.     private String phone;  
  4.     private String city;  
  5.     private Date createTime;  
  6.     private Date birthday;  
  7.     //将int改为Integer  
  8.      private Integer age;  

在Controller里面覆盖initBinder()方法。

 

[java] view plain copy  print?

  1.         /** 
  2.  * 覆盖initBinder()方法 
  3.  * 在执行请求参数到Command对象绑定之前,初始化一个可用的DataBinder实例,就是有了一个ServletRequestDataBinder之后,我们可以添加自定义的PropertyEdtior 以支持某些特殊数据类型的数据的绑定,或者排除某些不想绑定的请求参数,这些定制行为可以通过覆写initBinder()方法引入 。
  4.  * void registerCustomEditor(Class<?> requiredType,String propertyPath,PropertyEditor propertyEditor) 
  5.  * requiredType - the type of the property.  
  6.  * propertyPath - the path of the property (name or nested path), or null if registering an editor for all properties of the given type 
  7.  * propertyEditor - editor to register 
  8.  */  
  9.   
  10.   
  11. protected void initBinder(HttpServletRequest req,ServletRequestDataBinder binder)throws Exception{  
  12.     binder.registerCustomEditor(Integer.class"age"new IntegerEditor());  
  13.     binder.registerCustomEditor(Date.class,"birthday",new DateEditor());  
  14. }  

可以看出使用了binder.registerCustomEditor方法,它是用来注册的。所谓注册即告诉Spring,注册的属性由我来注入,不用你管了。可以看出我们注册了“age”和“birthday”属性。
那么这两个属性就由我们自己注入了。那么怎么注入呢,就是使用IntegerEditor()和DateEditor()方法。

下面是IntegerEditor()方法:

 

[java] view plain copy  print?

  1. public class IntegerEditor extends PropertyEditorSupport{  
  2.     @Override  
  3.     public String getAsText() {  
  4.         Integer value=(Integer)getValue();  
  5.         if(value==null){  
  6.             value=new Integer(0);  
  7.         }  
  8.         return value.toString();  
  9.     }  
  10.     @Override  
  11.     public void setAsText(String text) throws IllegalArgumentException {  
  12.         Integer value=null;  
  13.         if(text!=null&&!text.equals("")){  
  14.             value=Integer.valueOf(text);  
  15.         }  
  16.         setValue(value);  
  17.     }  
  18. }  

下面是DateEditor()方法:

[java] view plain copy  print?

  1. public class DateEditor extends PropertyEditorSupport{  
  2.     @Override  
  3.     public String getAsText() {  
  4.         Date value=(Date) getValue();  
  5.         if(value==null){  
  6.             value=new Date();  
  7.         }  
  8.         SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
  9.         return df.format(value);  
  10.     }  
  11.     @Override  
  12.     public void setAsText(String text) throws IllegalArgumentException {  
  13.         Date value=null;  
  14.         if(text!=null&&!text.equals("")){  
  15.             SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
  16.             try{  
  17.                 value=df.parse(text);  
  18.             }catch(Exception e){  
  19.                 e.printStackTrace();  
  20.             }  
  21.         }  
  22.         setValue(value);  
  23.     }  
  24. }  

getAsText和setAsText是要从新定义的。其中getAsText方法在get方法请求时会调用,而setAsText方法在post方法请求时会调用。

至此复杂Command的绑定原理了解了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一位远方的诗人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值