Monday, March 6, 2017

InitBinder for Long



This is to allow null value for Long
public void initBinder(WebDataBinder binder) {
 binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true) {
   @Override
   public void setAsText(String value) {
     if (value != null && !value.isEmpty() && Utility.isNumeric(value) ) {
       super.setAsText(value);
     } else {
      super.setAsText(null);
     }
   }
 });
}

Utility.isNumeric
public static boolean isNumeric(String str) {
  try {
   double d = Double.parseDouble(str);
  } catch (NumberFormatException nfe) {
   return false;
  }
  return true;
 }

No comments:

Post a Comment