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等许多,基本上够用。


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

        import org.springframework.beans.propertyeditors.PropertiesEditor;  
          
        public class DoubleEditor extends PropertiesEditor {    
            @Override    
            public void setAsText(String text) throws IllegalArgumentException {    
                if (text == null || text.equals("")) {    
                    text = "0";    
                }    
                setValue(Double.parseDouble(text));    
            }    
            
            @Override    
            public String getAsText() {    
                return getValue().toString();    
            }    
        }   
        import org.springframework.beans.propertyeditors.PropertiesEditor;  
          
        public class IntegerEditor extends PropertiesEditor {    
            @Override    
            public void setAsText(String text) throws IllegalArgumentException {    
                if (text == null || text.equals("")) {    
                    text = "0";    
                }    
                setValue(Integer.parseInt(text));    
            }    
            
            @Override    
            public String getAsText() {    
                return getValue().toString();    
            }    
        }   
     
        import org.springframework.beans.propertyeditors.PropertiesEditor;  
          
        public class FloatEditor extends PropertiesEditor {    
            @Override    
            public void setAsText(String text) throws IllegalArgumentException {    
                if (text == null || text.equals("")) {    
                    text = "0";    
                }    
                setValue(Float.parseFloat(text));    
            }    
            
            @Override    
            public String getAsText() {    
                return getValue().toString();    
            }    
        }    
     
        import org.springframework.beans.propertyeditors.PropertiesEditor;  
          
        public class LongEditor extends PropertiesEditor {    
            @Override    
            public void setAsText(String text) throws IllegalArgumentException {    
                if (text == null || text.equals("")) {    
                    text = "0";    
                }    
                setValue(Long.parseLong(text));    
            }    
            
            @Override    
            public String getAsText() {    
                return getValue().toString();    
            }    
        }    


    在BaseController中

     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        }   


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


    如果你的编辑器类直接继承PropertyEditorSupport也可以。

  • 相关阅读:
    06 PIE-Hyp图像修复
    在IIS中部署.NET Core WebApi程序
    深入学习ASP.NETCORE免费视频课程
    推荐设计10大接单平台
    Mysql—安装和使用(1)
    .Net WebApi接口之Swagger配置请求头apiKey验证
    .Net WebApi接口之Swagger设置让控制器上的注释显示
    执行dotnet *.dll启动项目,修改环境变量
    MySql下载及安装(Windows环境 )
    dev的grid封装组件,拖拽初始化属性
  • 原文地址:https://www.cnblogs.com/wzh123/p/5975107.html
Copyright © 2011-2022 走看看