Top Description Fields Constructors Methods
org.springframework.web.bind

public Class WebDataBinder

extends DataBinder
Class Inheritance
Known Direct Subclasses
org.springframework.web.bind.support.WebExchangeDataBinder, org.springframework.web.bind.support.WebRequestDataBinder, org.springframework.web.bind.ServletRequestDataBinder
Imports
java.lang.reflect.Array, java.util.Collection, .List, .Map, java.util.function.BiFunction, org.springframework.beans.MutablePropertyValues, .PropertyValue, org.springframework.core.CollectionFactory, org.springframework.lang.Nullable, org.springframework.validation.DataBinder, org.springframework.web.multipart.MultipartFile

Special 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 org.springframework.web.bind.ServletRequestDataBinder.

Warning

Data binding can lead to security issues by exposing parts of the object graph that are not meant to be accessed or modified by external clients. Therefore, the design and use of data binding should be considered carefully with regard to security. For more details, please refer to the dedicated sections on data binding for Spring Web MVC and Spring WebFlux in the reference manual.

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 than empty.

Authors
Juergen Hoeller, Scott Andrews, Brian Clozel
Since
1.2
See Also
registerCustomEditor, setAllowedFields, setRequiredFields, setFieldMarkerPrefix, setFieldDefaultPrefix, ServletRequestDataBinder

Field Summary

Modifier and TypeField and Description
private boolean
public static final String
DEFAULT_FIELD_DEFAULT_PREFIX

Default prefix that field default parameters start with, followed by the field name: e.g. "!subscribeToNewsletter" for a field "subscribeToNewsletter".

public static final String
DEFAULT_FIELD_MARKER_PREFIX

Default prefix that field marker parameters start with, followed by the field name: e.g. "_subscribeToNewsletter" for a field "subscribeToNewsletter".

private String
private String
Inherited from org.springframework.validation.DataBinder:
DEFAULT_AUTO_GROW_COLLECTION_LIMITDEFAULT_OBJECT_NAMElogger

Constructor Summary

AccessConstructor and Description
public
WebDataBinder(Object
the target object to bind onto (or null if the binder is just used to convert a plain parameter value)
target
)

Create a new WebDataBinder instance, with default object name.

public
WebDataBinder(Object
the target object to bind onto (or null if the binder is just used to convert a plain parameter value)
target
,
String
the name of the target object
objectName
)

Create a new WebDataBinder instance.

Method Summary

Modifier and TypeMethod and Description
protected void
adaptEmptyArrayIndices(MutablePropertyValues
the property values to be bound (can be modified)
mpvs
)

Check for property values with names that end on "[]".

protected void
bindMultipart(Map<String, List<MultipartFile>>
a Map of field name String to MultipartFile object
multipartFiles
,
MutablePropertyValues
the property values to be bound (can be modified)
mpvs
)

Bind all multipart files contained in the given request, if any (in case of a multipart request).

protected void
checkFieldDefaults(MutablePropertyValues
the property values to be bound (can be modified)
mpvs
)

Check the given property values for field defaults, i.e. for fields that start with the field default prefix.

protected void
checkFieldMarkers(MutablePropertyValues
the property values to be bound (can be modified)
mpvs
)

Check the given property values for field markers, i.e. for fields that start with the field marker prefix.

protected void
doBind(MutablePropertyValues
the property values to bind, as MutablePropertyValues instance
mpvs
)

Overrides org.springframework.validation.DataBinder.doBind.

This implementation performs a field default and marker check before delegating to the superclass binding process.
protected Object

Returns:

the empty value (for most fields: null)
getEmptyValue
(String
the name of the field
field
,
Class<?>
the type of the field
fieldType
)

Determine an empty value for the specified field.

public Object

Returns:

the empty value (for most fields: null)
getEmptyValue
(Class<?>
the type of the field
fieldType
)

Determine an empty value for the specified field.

public String
getFieldDefaultPrefix()

Return the prefix for parameters that mark default fields.

public String
getFieldMarkerPrefix()

Return the prefix for parameters that mark potentially empty fields.

public boolean
isBindEmptyMultipartFiles()

Return whether to bind empty MultipartFile parameters.

protected Object

Returns:

the resolved value, or null
resolvePrefixValue
(String
the name of the value to resolve
name
,
Class<?>
the type of value expected
type
,
BiFunction<String, Class<?>, Object>
delegate resolver to use for the checks
resolver
)

Check if a value can be resolved if getFieldDefaultPrefix() or getFieldMarkerPrefix() is prepended.

public void
setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)

Set whether to bind empty MultipartFile parameters.

public void
setFieldDefaultPrefix(String fieldDefaultPrefix)

Specify a prefix that can be used for parameters that indicate default value fields, having "prefix + field" as name.

public void
setFieldMarkerPrefix(String fieldMarkerPrefix)

Specify a prefix that can be used for parameters that mark potentially empty fields, having "prefix + field" as name.

Inherited from org.springframework.validation.DataBinder:
addCustomFormatteraddCustomFormatteraddCustomFormatteraddValidatorsapplyPropertyValuesbindcheckAllowedFieldscheckRequiredFieldscloseconstructconvertIfNecessaryconvertIfNecessaryconvertIfNecessaryconvertIfNecessarycreateBeanPropertyBindingResultcreateDirectFieldBindingResultfindCustomEditorgetAllowedFieldsgetAutoGrowCollectionLimitgetBindingErrorProcessorgetBindingResultgetConversionServicegetDisallowedFieldsgetInternalBindingResultgetNameResolvergetObjectNamegetPropertyAccessorgetPropertyEditorRegistrygetRequiredFieldsgetSimpleTypeConvertergetTargetgetTargetTypegetTypeConvertergetValidatorgetValidatorsgetValidatorsToApplyinitBeanPropertyAccessinitDirectFieldAccessisAllowedisAutoGrowNestedPathsisDeclarativeBindingisIgnoreInvalidFieldsisIgnoreUnknownFieldsregisterCustomEditorregisterCustomEditorreplaceValidatorssetAllowedFieldssetAutoGrowCollectionLimitsetAutoGrowNestedPathssetBindingErrorProcessorsetConversionServicesetDeclarativeBindingsetDisallowedFieldssetExcludedValidatorssetIgnoreInvalidFieldssetIgnoreUnknownFieldssetMessageCodesResolversetNameResolversetRequiredFieldssetTargetTypesetValidatorshouldConstructArgumentshouldNotBindPropertyValuesvalidatevalidate

Field Detail

bindEmptyMultipartFilesback to summary
private boolean bindEmptyMultipartFiles
DEFAULT_FIELD_DEFAULT_PREFIXback to summary
public static final String DEFAULT_FIELD_DEFAULT_PREFIX

Default prefix that field default parameters start with, followed by the field name: e.g. "!subscribeToNewsletter" for a field "subscribeToNewsletter".

Default parameters differ from field markers in that they provide a default value instead of an empty value.

See Also
setFieldDefaultPrefix
DEFAULT_FIELD_MARKER_PREFIXback to summary
public static final String DEFAULT_FIELD_MARKER_PREFIX

Default prefix that field marker parameters start with, followed by the field name: e.g. "_subscribeToNewsletter" for a field "subscribeToNewsletter".

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.

See Also
setFieldMarkerPrefix
fieldDefaultPrefixback to summary
private String fieldDefaultPrefix
Annotations
@Nullable
fieldMarkerPrefixback to summary
private String fieldMarkerPrefix
Annotations
@Nullable

Constructor Detail

WebDataBinderback to summary
public WebDataBinder(Object target)

Create a new WebDataBinder instance, with default object name.

Parameters
target:Object

the target object to bind onto (or null if the binder is just used to convert a plain parameter value)

See Also
DEFAULT_OBJECT_NAME
WebDataBinderback to summary
public WebDataBinder(Object target, String objectName)

Create a new WebDataBinder instance.

Parameters
target:Object

the target object to bind onto (or null if the binder is just used to convert a plain parameter value)

objectName:String

the name of the target object

Method Detail

adaptEmptyArrayIndicesback to summary
protected void adaptEmptyArrayIndices(MutablePropertyValues mpvs)

Check for property values with names that end on "[]". This is used by some clients for array syntax without an explicit index value. If such values are found, drop the brackets to adapt to the expected way of expressing the same for data binding purposes.

Parameters
mpvs:MutablePropertyValues

the property values to be bound (can be modified)

Since
5.3
bindMultipartback to summary
protected void bindMultipart(Map<String, List<MultipartFile>> multipartFiles, MutablePropertyValues mpvs)

Bind all multipart files contained in the given request, if any (in case of a multipart request). To be called by subclasses.

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.

Parameters
multipartFiles:Map<String, List<MultipartFile>>

a Map of field name String to MultipartFile object

mpvs:MutablePropertyValues

the property values to be bound (can be modified)

See Also
org.springframework.web.multipart.MultipartFile, setBindEmptyMultipartFiles
checkFieldDefaultsback to summary
protected void checkFieldDefaults(MutablePropertyValues mpvs)

Check the given property values for field defaults, i.e. for fields that start with the field default prefix.

The existence of a field defaults indicates that the specified value should be used if the field is otherwise not present.

Parameters
mpvs:MutablePropertyValues

the property values to be bound (can be modified)

See Also
getFieldDefaultPrefix
checkFieldMarkersback to summary
protected void checkFieldMarkers(MutablePropertyValues mpvs)

Check the given property values for field markers, i.e. for fields that start with the field marker prefix.

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.

Parameters
mpvs:MutablePropertyValues

the property values to be bound (can be modified)

See Also
getFieldMarkerPrefix, getEmptyValue(String, Class)
doBindback to summary
protected void doBind(MutablePropertyValues mpvs)

Overrides org.springframework.validation.DataBinder.doBind.

This implementation performs a field default and marker check before delegating to the superclass binding process.

Parameters
mpvs:MutablePropertyValues

Doc from org.springframework.validation.DataBinder.doBind.

the property values to bind, as MutablePropertyValues instance

Annotations
@Override
See Also
checkFieldDefaults, checkFieldMarkers
getEmptyValueback to summary
protected Object getEmptyValue(String field, Class<?> fieldType)

Determine an empty value for the specified field.

The default implementation delegates to getEmptyValue(Class) if the field type is known, otherwise falls back to null.

Parameters
field:String

the name of the field

fieldType:Class<?>

the type of the field

Returns:Object

the empty value (for most fields: null)

Annotations
@Nullable
getEmptyValueback to summary
public Object getEmptyValue(Class<?> fieldType)

Determine an empty value for the specified field.

The default implementation returns:

  • Boolean.FALSE for boolean fields
  • an empty array for array types
  • Collection implementations for Collection types
  • Map implementations for Map types
  • else, null is used as default
Parameters
fieldType:Class<?>

the type of the field

Returns:Object

the empty value (for most fields: null)

Annotations
@Nullable
Since
5.0
getFieldDefaultPrefixback to summary
public String getFieldDefaultPrefix()

Return the prefix for parameters that mark default fields.

Annotations
@Nullable
getFieldMarkerPrefixback to summary
public String getFieldMarkerPrefix()

Return the prefix for parameters that mark potentially empty fields.

Annotations
@Nullable
isBindEmptyMultipartFilesback to summary
public boolean isBindEmptyMultipartFiles()

Return whether to bind empty MultipartFile parameters.

resolvePrefixValueback to summary
protected Object resolvePrefixValue(String name, Class<?> type, BiFunction<String, Class<?>, Object> resolver)

Check if a value can be resolved if getFieldDefaultPrefix() or getFieldMarkerPrefix() is prepended.

Parameters
name:String

the name of the value to resolve

type:Class<?>

the type of value expected

resolver:BiFunction<String, Class<?>, Object>

delegate resolver to use for the checks

Returns:Object

the resolved value, or null

Annotations
@Nullable
Since
6.1
setBindEmptyMultipartFilesback to summary
public void setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)

Set whether to bind empty MultipartFile parameters. Default is "true".

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.

See Also
org.springframework.web.multipart.MultipartFile
setFieldDefaultPrefixback to summary
public void setFieldDefaultPrefix(String fieldDefaultPrefix)

Specify a prefix that can be used for parameters that indicate default value fields, having "prefix + field" as name. The value of the default field is used when the field is not provided.

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.

See Also
DEFAULT_FIELD_DEFAULT_PREFIX
setFieldMarkerPrefixback to summary
public void setFieldMarkerPrefix(String fieldMarkerPrefix)

Specify a prefix that can be used for parameters that mark potentially empty fields, having "prefix + field" as name. Such a marker parameter is checked by existence: You can send any value for it, for example "visible". This is particularly useful for HTML checkboxes and select options.

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.

See Also
DEFAULT_FIELD_MARKER_PREFIX