zoukankan      html  css  js  c++  java
  • spring mvc使用@InitBinder 标签对表单数据绑定

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

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

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

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

    [java] view plain copy
     
    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. }   
    [java] view plain copy
     
    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. }   
    [java] view plain copy
     
    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. }    
    [java] view plain copy
     
    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中

    [java] view plain copy
     
    1. @InitBinder    
    2.    protected void initBinder(WebDataBinder binder) {    
    3.        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));    
    4. /        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));    
    5.        binder.registerCustomEditor(int.class, new IntegerEditor());    
    6. /        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));  
    7.        binder.registerCustomEditor(long.class, new LongEditor());    
    8.        binder.registerCustomEditor(double.class, new DoubleEditor());    
    9.        binder.registerCustomEditor(float.class, new FloatEditor());    
    10.    }   
    [java] view plain copy
     
      1. public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {  
  • 相关阅读:
    Golang Failpoint 的设计与实现
    没涉及到最值求解;观点:矩阵乘法无法表达出结果。 现实生活中事件、现象的数学表达
    多元微分学 枚举破解15位路由器密码 存储空间限制 拆分减长,求最值 数据去重
    ARP Poisoning Attack and Mitigation Techniques ARP欺骗 中间人攻击 Man-In-The-Middle (MITM) attack 嗅探 防范 Can one MAC address have two different IP addresses within the network?
    The C10K problem
    HTTP Streaming Architecture HLS 直播点播 HTTP流架构
    现代IM系统中消息推送和存储架构的实现
    现代IM系统中的消息系统架构
    长连接锁服务优化实践 C10K问题 nodejs的内部构造 limits.conf文件修改 sysctl.conf文件修改
    doubleclick cookie、动态脚本、用户画像、用户行为分析和海量数据存取 推荐词 京东 电商 信息上传 黑洞 https://blackhole.m.jd.com/getinfo
  • 原文地址:https://www.cnblogs.com/xing1/p/8416539.html
Copyright © 2011-2022 走看看