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

 更新时间:2018年03月14日 15:45:24   作者:lemrose  
这篇文章主要介绍了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中

@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也可以。

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

相关文章

  • mybatis通过TypeHandler list转换string类型转换方式

    mybatis通过TypeHandler list转换string类型转换方式

    这篇文章主要介绍了mybatis通过TypeHandler list转换string类型转换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java 面试题基础知识集锦

    Java 面试题基础知识集锦

    本文主要介绍Java基础面试题集锦,这里整理了面试java工程师的基础知识题锦,有需要的小伙伴可以参考下
    2016-09-09
  • springmvc+spring+mybatis实现用户登录功能(上)

    springmvc+spring+mybatis实现用户登录功能(上)

    这篇文章主要为大家详细介绍了springmvc+spring+mybatis实现用户登录功能,比较基础的学习教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Springboot实现前后端分离excel下载

    Springboot实现前后端分离excel下载

    这篇文章主要介绍了Springboot实现前后端分离excel下载,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java文件下载ZIP报错:Out of Memory的问题排查

    Java文件下载ZIP报错:Out of Memory的问题排查

    本文主要介绍了Java项目中下载大文件(超过2G的ZIP文件)时出现内存溢出(OutOfMemory:JavaHeapSpace)的问题,具有一定的参考价值,感兴趣的可以了解一下
    2025-01-01
  • Java中EnumMap的使用解析

    Java中EnumMap的使用解析

    这篇文章主要介绍了Java中EnumMap的使用解析,EnumMap 是一种特殊的 Map,它要求自身所有的键来自某个枚举类型,EnumMap 的内部可以作为一个数组来实现,因此它们的性能非常好,你可以放心地用 EnumMap 来实现基于枚举的查询,需要的朋友可以参考下
    2023-11-11
  • SpringBoot整合消息队列RabbitMQ

    SpringBoot整合消息队列RabbitMQ

    SpringBoot整合RabbitMQ很容易,但是整合的目的是为了使用,那要使用RabbitMQ就要对其有一定的了解,不然容易整成一团浆糊。因为说到底,SpringBoot只是在封装RabbitMQ的API,让其更容易使用而已,废话不多说,让我们一起整它
    2023-03-03
  • IntelliJ IDEA窗口组件具体操作方法

    IntelliJ IDEA窗口组件具体操作方法

    IDEA刚接触不久,各种常用工具窗口找不到,不小心关掉不知道从哪里打开,今天小编给大家分享这个问题的解决方法,感兴趣的朋友一起看看吧
    2021-09-09
  • Java+EasyExcel实现文件的导入导出

    Java+EasyExcel实现文件的导入导出

    在项目中我们常常需要Excel文件的导入与导出,手动输入相对有些繁琐,所以本文教大家如何在Java中轻松导入与导出Excel文件,感兴趣的可以学习一下
    2021-12-12
  • SpringBoot中的Condition包下常用条件依赖注解案例介绍

    SpringBoot中的Condition包下常用条件依赖注解案例介绍

    这篇文章主要介绍了SpringBoot中的Condition包下常用条件依赖注解案例,文章基于Java的相关资料展开主题详细内容,需要的小伙伴可以参考一下
    2022-04-04

最新评论