服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring mvc使用@InitBinder标签对表单数据绑定的方法

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

2021-04-12 09:20lemrose Java教程

这篇文章主要介绍了spring mvc使用@InitBinder标签对表单数据绑定的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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(); 
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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(); 
  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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(); 
  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
2
3
4
5
6
7
8
9
10
@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也可以。

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

原文链接:http://blog.csdn.net/axin66ok/article/details/17938095

延伸 · 阅读

精彩推荐