spring mvc使用@InitBinder 标签对表单数据绑定

在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能。

比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作

  1. @InitBinder  
  2.     public void initBinder(WebDataBinder binder) {  
  3.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  4.         dateFormat.setLenient(false);  
  5.         binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, true));  
  6.     }  
还要在springMVC配置文件中加上

  1. <!-- 解析器注册 -->  
  2.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
  3.         <property name="messageConverters">  
  4.             <list>  
  5.                 <ref bean="stringHttpMessageConverter"/>  
  6.             </list>  
  7.         </property>  
  8.     </bean>  
  9.     <!-- String类型解析器,允许直接返回String类型的消息 -->  
  10.     <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>  

这样就可以直接将上传的日期时间字符串绑定为日期类型的数据了



在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。

解决的办法就是使用spring mvc提供的@InitBinder标签

在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用


当然,我们也可以不使用他自己自带这些编辑器类,那下面我们自己去构造几个

  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class DoubleEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Double.parseDouble(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }   

  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class IntegerEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Integer.parseInt(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }   

  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class FloatEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Float.parseFloat(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }    

  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class LongEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Long.parseLong(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }    

在BaseController中

  1. @InitBinder    
  2.    protected void initBinder(WebDataBinder binder) {    
  3.        binder.registerCustomEditor(Date.classnew CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));    
  4. /        binder.registerCustomEditor(int.classnew CustomNumberEditor(int.classtrue));    
  5.        binder.registerCustomEditor(int.classnew IntegerEditor());    
  6. /        binder.registerCustomEditor(long.classnew CustomNumberEditor(long.classtrue));  
  7.        binder.registerCustomEditor(long.classnew LongEditor());    
  8.        binder.registerCustomEditor(double.classnew DoubleEditor());    
  9.        binder.registerCustomEditor(float.classnew FloatEditor());    
  10.    }   


  1. public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {  

看到没?如果你的编辑器类直接继承PropertyEditorSupport也可以。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值