打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring3,controller类中接收前台传来的对象中空整型或日期的处理
2011-06-21 09:25

 在controller类中增加@InitBinder部分的代码就可以解决前台传来的对象中空整型或日期的问题

@InitBinder 

public void initBinder(WebDataBinder binder) {   

 binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {  

      public void setAsText(String value) {  

         try {

   setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));

 }catch (java.text.ParseException e) {

setValue(null);  

 }              

      }

      

      public String getAsText() {  

           return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());  

      }  

   });  

  binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor( 

    BigDecimal.class, false)); 

  binder.registerCustomEditor(Integer.class, null, 

    new CustomNumberEditor(Integer.class, null, true)); 

  binder.registerCustomEditor(int.class, null, new CustomNativeEditor( 

  Integer.class, null , true)); 

  binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( 

    Long.class, null, true)); 

  binder.registerCustomEditor(long.class, null, new CustomNativeEditor( 

    Long.class, null, true)); 

  binder.registerCustomEditor(Float.class, new CustomNumberEditor( 

    Float.class, true)); 

  binder.registerCustomEditor(Double.class, new CustomNumberEditor( 

    Double.class, true)); 

  binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor( 

    BigInteger.class, true)); 

}  

 

 

 

-----------CustomNativeEditor.java------------------------------------------------

 

 

//这个类上面的InitBinder 的方法中会使用到;

 

import java.beans.PropertyEditorSupport;

import java.text.NumberFormat; 

 

import org.springframework.util.NumberUtils;

import org.springframework.util.StringUtils;

 

public class CustomNativeEditor extends PropertyEditorSupport { 

  

  private final Class numberClass; 

  

  private final NumberFormat numberFormat; 

  

  private final boolean allowEmpty;  

 

  public CustomNativeEditor(Class numberClass, boolean allowEmpty) throws IllegalArgumentException { 

  this(numberClass, null, allowEmpty); 

  } 

  

  public CustomNativeEditor(Class numberClass, NumberFormat numberFormat, boolean allowEmpty) 

      throws IllegalArgumentException { 

  

  if (numberClass == null || !Number.class.isAssignableFrom(numberClass)) { 

  throw new IllegalArgumentException("Property class must be a subclass of Number"); 

  } 

  this.numberClass = numberClass; 

  this.numberFormat = numberFormat; 

  this.allowEmpty = allowEmpty; 

  } 

  

  

 

  @Override 

  @SuppressWarnings("unchecked") 

  public void setAsText(String text) throws IllegalArgumentException {

if (this.allowEmpty && !StringUtils.hasText(text)) {

  // Treat empty String as null value.

  setValue(0);//这里就是整型为对空值的处理;

} else if (this.numberFormat != null) {

  // Use given NumberFormat for parsing text.

  setValue(NumberUtils.parseNumber(text, this.numberClass,

    this.numberFormat));

} else {

  // Use default valueOf methods for parsing text.

  setValue(NumberUtils.parseNumber(text, this.numberClass));

}

  }

  

  /** 

   * Coerce a Number value into the required target class, if necessary. 

   */ 

  @Override 

  @SuppressWarnings("unchecked") 

  public void setValue(Object value) { 

  if (value instanceof Number) { 

  super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass)); 

  } 

  else { 

  super.setValue(value); 

  } 

  } 

  

  /** 

   * Format the Number as String, using the specified NumberFormat. 

   */ 

  @Override 

  public String getAsText() { 

  Object value = getValue(); 

  if (value == null) { 

  return ""; 

  } 

  if (this.numberFormat != null) { 

  // Use NumberFormat for rendering value. 

  return this.numberFormat.format(value); 

  } 

  else { 

  // Use toString method for rendering value. 

  return value.toString(); 

  } 

  } 

  

  } 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java – Spring MVC表单:options标签没有连接到我的对象Id?
第四章 Controller接口控制器详解(6)——跟着开涛学SpringMVC
源码剖析@ApiImplicitParam对@RequestParam的required属性侵入性
Spring MVC中基于自定义Editor的表单数据处理技巧分享
C#基础系列:小话泛型
超轻量级MVC框架的设计和实现 (2)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服