public class WebDataBinder extends DataBinder
DataBinder
for data binding from web request parameters
to JavaBean objects. Designed for web environments, but not dependent on
the Servlet API; serves as base class for more specific DataBinder variants,
such as ServletRequestDataBinder
.
Includes support for field markers which address a common problem with HTML checkboxes and select options: detecting that a field was part of the form, but did not generate a request parameter because it was empty. A field marker allows to detect that state and reset the corresponding bean property accordingly. Default values, for parameters that are otherwise not present, can specify a value for the field other then empty.
DataBinder.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)
,
DataBinder.setAllowedFields(java.lang.String...)
,
DataBinder.setRequiredFields(java.lang.String...)
,
setFieldMarkerPrefix(java.lang.String)
,
setFieldDefaultPrefix(java.lang.String)
,
ServletRequestDataBinder
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_FIELD_DEFAULT_PREFIX
Default prefix that field default parameters start with, followed by the field
name: e.g.
|
static java.lang.String |
DEFAULT_FIELD_MARKER_PREFIX
Default prefix that field marker parameters start with, followed by the field
name: e.g.
|
DEFAULT_AUTO_GROW_COLLECTION_LIMIT, DEFAULT_OBJECT_NAME, logger
Constructor and Description |
---|
WebDataBinder(java.lang.Object target)
Create a new WebDataBinder instance, with default object name.
|
WebDataBinder(java.lang.Object target,
java.lang.String objectName)
Create a new WebDataBinder instance.
|
Modifier and Type | Method and Description |
---|---|
protected void |
bindMultipart(java.util.Map<java.lang.String,java.util.List<MultipartFile>> multipartFiles,
MutablePropertyValues mpvs)
Bind all multipart files contained in the given request, if any
(in case of a multipart request).
|
protected void |
checkFieldDefaults(MutablePropertyValues mpvs)
Check the given property values for field defaults,
i.e.
|
protected void |
checkFieldMarkers(MutablePropertyValues mpvs)
Check the given property values for field markers,
i.e.
|
protected void |
doBind(MutablePropertyValues mpvs)
This implementation performs a field default and marker check
before delegating to the superclass binding process.
|
java.lang.Object |
getEmptyValue(java.lang.Class<?> fieldType)
Determine an empty value for the specified field.
|
protected java.lang.Object |
getEmptyValue(java.lang.String field,
java.lang.Class<?> fieldType)
Determine an empty value for the specified field.
|
java.lang.String |
getFieldDefaultPrefix()
Return the prefix for parameters that mark default fields.
|
java.lang.String |
getFieldMarkerPrefix()
Return the prefix for parameters that mark potentially empty fields.
|
boolean |
isBindEmptyMultipartFiles()
Return whether to bind empty MultipartFile parameters.
|
void |
setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)
Set whether to bind empty MultipartFile parameters.
|
void |
setFieldDefaultPrefix(java.lang.String fieldDefaultPrefix)
Specify a prefix that can be used for parameters that indicate default
value fields, having "prefix + field" as name.
|
void |
setFieldMarkerPrefix(java.lang.String fieldMarkerPrefix)
Specify a prefix that can be used for parameters that mark potentially
empty fields, having "prefix + field" as name.
|
addCustomFormatter, addCustomFormatter, addCustomFormatter, addValidators, applyPropertyValues, bind, checkAllowedFields, checkRequiredFields, close, convertIfNecessary, convertIfNecessary, convertIfNecessary, createBeanPropertyBindingResult, createDirectFieldBindingResult, findCustomEditor, getAllowedFields, getAutoGrowCollectionLimit, getBindingErrorProcessor, getBindingResult, getConversionService, getDisallowedFields, getInternalBindingResult, getObjectName, getPropertyAccessor, getPropertyEditorRegistry, getRequiredFields, getSimpleTypeConverter, getTarget, getTypeConverter, getValidator, getValidators, initBeanPropertyAccess, initDirectFieldAccess, isAllowed, isAutoGrowNestedPaths, isIgnoreInvalidFields, isIgnoreUnknownFields, registerCustomEditor, registerCustomEditor, replaceValidators, setAllowedFields, setAutoGrowCollectionLimit, setAutoGrowNestedPaths, setBindingErrorProcessor, setConversionService, setDisallowedFields, setIgnoreInvalidFields, setIgnoreUnknownFields, setMessageCodesResolver, setRequiredFields, setValidator, validate, validate
public static final java.lang.String DEFAULT_FIELD_MARKER_PREFIX
Such a marker parameter indicates that the field was visible, that is, existed in the form that caused the submission. If no corresponding field value parameter was found, the field will be reset. The value of the field marker parameter does not matter in this case; an arbitrary value can be used. This is particularly useful for HTML checkboxes and select options.
public static final java.lang.String DEFAULT_FIELD_DEFAULT_PREFIX
Default parameters differ from field markers in that they provide a default value instead of an empty value.
public WebDataBinder(@Nullable java.lang.Object target)
target
- the target object to bind onto (or null
if the binder is just used to convert a plain parameter value)DataBinder.DEFAULT_OBJECT_NAME
public WebDataBinder(@Nullable java.lang.Object target, java.lang.String objectName)
target
- the target object to bind onto (or null
if the binder is just used to convert a plain parameter value)objectName
- the name of the target objectpublic void setFieldMarkerPrefix(@Nullable java.lang.String fieldMarkerPrefix)
Default is "_", for "_FIELD" parameters (e.g. "_subscribeToNewsletter"). Set this to null if you want to turn off the empty field check completely.
HTML checkboxes only send a value when they're checked, so it is not possible to detect that a formerly checked box has just been unchecked, at least not with standard HTML means.
One way to address this is to look for a checkbox parameter value if
you know that the checkbox has been visible in the form, resetting the
checkbox if no value found. In Spring web MVC, this typically happens
in a custom onBind
implementation.
This auto-reset mechanism addresses this deficiency, provided that a marker parameter is sent for each checkbox field, like "_subscribeToNewsletter" for a "subscribeToNewsletter" field. As the marker parameter is sent in any case, the data binder can detect an empty field and automatically reset its value.
DEFAULT_FIELD_MARKER_PREFIX
@Nullable public java.lang.String getFieldMarkerPrefix()
public void setFieldDefaultPrefix(@Nullable java.lang.String fieldDefaultPrefix)
Default is "!", for "!FIELD" parameters (e.g. "!subscribeToNewsletter"). Set this to null if you want to turn off the field defaults completely.
HTML checkboxes only send a value when they're checked, so it is not possible to detect that a formerly checked box has just been unchecked, at least not with standard HTML means. A default field is especially useful when a checkbox represents a non-boolean value.
The presence of a default parameter preempts the behavior of a field marker for the given field.
DEFAULT_FIELD_DEFAULT_PREFIX
@Nullable public java.lang.String getFieldDefaultPrefix()
public void setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)
Turn this off if you want to keep an already bound MultipartFile when the user resubmits the form without choosing a different file. Else, the already bound MultipartFile will be replaced by an empty MultipartFile holder.
MultipartFile
public boolean isBindEmptyMultipartFiles()
protected void doBind(MutablePropertyValues mpvs)
doBind
in class DataBinder
mpvs
- the property values to bind,
as MutablePropertyValues instancecheckFieldDefaults(org.springframework.beans.MutablePropertyValues)
,
checkFieldMarkers(org.springframework.beans.MutablePropertyValues)
protected void checkFieldDefaults(MutablePropertyValues mpvs)
The existence of a field defaults indicates that the specified value should be used if the field is otherwise not present.
mpvs
- the property values to be bound (can be modified)getFieldDefaultPrefix()
protected void checkFieldMarkers(MutablePropertyValues mpvs)
The existence of a field marker indicates that the specified field existed in the form. If the property values do not contain a corresponding field value, the field will be considered as empty and will be reset appropriately.
mpvs
- the property values to be bound (can be modified)getFieldMarkerPrefix()
,
getEmptyValue(String, Class)
@Nullable protected java.lang.Object getEmptyValue(java.lang.String field, @Nullable java.lang.Class<?> fieldType)
The default implementation delegates to getEmptyValue(Class)
if the field type is known, otherwise falls back to null
.
field
- the name of the fieldfieldType
- the type of the fieldnull
)@Nullable public java.lang.Object getEmptyValue(java.lang.Class<?> fieldType)
The default implementation returns:
Boolean.FALSE
for boolean fields
null
is used as default
fieldType
- the type of the fieldnull
)protected void bindMultipart(java.util.Map<java.lang.String,java.util.List<MultipartFile>> multipartFiles, MutablePropertyValues mpvs)
Multipart files will only be added to the property values if they are not empty or if we're configured to bind empty multipart files too.
multipartFiles
- a Map of field name String to MultipartFile objectmpvs
- the property values to be bound (can be modified)MultipartFile
,
setBindEmptyMultipartFiles(boolean)