当前位置: 移动技术网 > IT编程>开发语言>Java > spring mvc使用@InitBinder标签对表单数据绑定的方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

在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中

@initbinder  
  protected void initbinder(webdatabinder binder) {  
    binder.registercustomeditor(date.class, new customdateeditor(new simpledateformat("yyyy-mm-dd hh:mm:ss"), true));  
/    binder.registercustomeditor(int.class, new customnumbereditor(int.class, true));  
    binder.registercustomeditor(int.class, new integereditor());  
/    binder.registercustomeditor(long.class, new customnumbereditor(long.class, true)); 
    binder.registercustomeditor(long.class, new longeditor());  
    binder.registercustomeditor(double.class, new doubleeditor());  
    binder.registercustomeditor(float.class, new floateditor());  
  }  

复制代码 代码如下:

public class org.springframework.beans.propertyeditors.propertieseditor extends java.beans.propertyeditorsupport { 

看到没?如果你的编辑器类直接继承propertyeditorsupport也可以。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网