在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();
}
}
}
联系客服