Support new default values in WebDataBinder
Prior to this commit, binding a `@ModelAttribute` object as a Controller handler paramater would instantiate the object and set all its properties, fetching data from the request. When no data is available, the WebDataBinder tries to bind default "empty" values: * Boolean.FALSE for boolean types * empty arrays for array types * null by default This commit adds the new default empty values: * empty Collections for Collection types * empty Maps for Map types Rather than using empty implementations provided by `Collections.empty` (which are not mutable), we're using the closest possible target type and real implementations, provided by the `CollectionFactory`. Issue: SPR-13502
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,11 +17,13 @@
|
||||
package org.springframework.web.bind;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.validation.DataBinder;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -40,6 +42,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Scott Andrews
|
||||
* @author Brian Clozel
|
||||
* @since 1.2
|
||||
* @see #registerCustomEditor
|
||||
* @see #setAllowedFields
|
||||
@@ -243,26 +246,41 @@ public class WebDataBinder extends DataBinder {
|
||||
|
||||
/**
|
||||
* Determine an empty value for the specified field.
|
||||
* <p>Default implementation returns {@code Boolean.FALSE}
|
||||
* for boolean fields and an empty array of array types.
|
||||
* Else, {@code null} is used as default.
|
||||
* <p>Default implementation returns:
|
||||
* <ul>
|
||||
* <li>{@code Boolean.FALSE} for boolean fields
|
||||
* <li>an empty array for array types
|
||||
* <li>Collection implementations for Collection types
|
||||
* <li>Map implementations for Map types
|
||||
* <li>else, {@code null} is used as default
|
||||
* </ul>
|
||||
* @param field the name of the field
|
||||
* @param fieldType the type of the field
|
||||
* @return the empty value (for most fields: null)
|
||||
*/
|
||||
protected Object getEmptyValue(String field, Class<?> fieldType) {
|
||||
if (fieldType != null && boolean.class == fieldType || Boolean.class == fieldType) {
|
||||
// Special handling of boolean property.
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
else if (fieldType != null && fieldType.isArray()) {
|
||||
// Special handling of array property.
|
||||
return Array.newInstance(fieldType.getComponentType(), 0);
|
||||
}
|
||||
else {
|
||||
// Default value: try null.
|
||||
return null;
|
||||
if (fieldType != null) {
|
||||
try {
|
||||
if (boolean.class == fieldType || Boolean.class == fieldType) {
|
||||
// Special handling of boolean property.
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
else if (fieldType.isArray()) {
|
||||
// Special handling of array property.
|
||||
return Array.newInstance(fieldType.getComponentType(), 0);
|
||||
}
|
||||
else if (Collection.class.isAssignableFrom(fieldType)) {
|
||||
return CollectionFactory.createCollection(fieldType, 0);
|
||||
}
|
||||
else if (Map.class.isAssignableFrom(fieldType)) {
|
||||
return CollectionFactory.createMap(fieldType, 0);
|
||||
}
|
||||
} catch (IllegalArgumentException exc) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// Default value: try null.
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user