Package org.springframework.web.bind

# Class ServletRequestDataBinder

java.lang.Object
org.springframework.validation.DataBinder
org.springframework.web.bind.WebDataBinder
org.springframework.web.bind.ServletRequestDataBinder

All Implemented Interfaces:
  `org.springframework.beans.PropertyEditorRegistry`, `org.springframework.beans.TypeConverter`

public class ServletRequestDataBinder extends WebDataBinder
Special `DataBinder` to perform data binding from servlet request parameters to JavaBeans, including support for multipart files.

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.

See the DataBinder/WebDataBinder superclasses for customization options, which include specifying allowed/required fields, and registering custom property editors.

Can also be used for manual data binding. Simply instantiate a ServletRequestDataBinder for each binding process, and invoke `bind` with the current ServletRequest as argument:

     MyBean myBean = new MyBean();
     // apply binder to custom target object
     ServletRequestDataBinder binder = new ServletRequestDataBinder(myBean);
     // register custom editors, if desired
     binder.registerCustomEditor(...);
     // trigger actual binding of request parameters
     binder.bind(request);
     // optionally evaluate binding errors
     BindingResult bindingResult = binder.getBindingResult();
     ...

Author:
  Rod Johnson, Juergen Hoeller
See Also:
  - `bind(jakarta.servlet.ServletRequest)`
  - `DataBinder.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)`
  - `DataBinder.setAllowedFields(java.lang.String...)`
  - `DataBinder.setRequiredFields(java.lang.String...)`
  - `WebDataBinder.setFieldMarkerPrefix(java.lang.String)`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected static class`
`ServletRequestDataBinder.ServletRequestValueResolver`
Resolver that looks up values to bind in a `ServletRequest`.

## Nested classes/interfaces inherited from class org.springframework.validation.DataBinder

`org.springframework.validation.DataBinder.NameResolver, org.springframework.validation.DataBinder.ValueResolver`

## Field Summary

### Fields inherited from class org.springframework.web.bind.WebDataBinder

`DEFAULT_FIELD_DEFAULT_PREFIX, DEFAULT_FIELD_MARKER_PREFIX`

### Fields inherited from class org.springframework.validation.DataBinder

`DEFAULT_AUTO_GROW_COLLECTION_LIMIT, DEFAULT_OBJECT_NAME, logger`

## Constructor Summary

Constructors
Constructor
Description
`ServletRequestDataBinder(Object target)`
Create a new ServletRequestDataBinder instance, with default object name.
`ServletRequestDataBinder(Object target, String objectName)`
Create a new ServletRequestDataBinder instance.

## Method Summary

Modifier and Type
Method
Description
`protected void`
`addBindValues(org.springframework.beans.MutablePropertyValues mpvs, ServletRequest request)`
Extension point that subclasses can use to add extra bind values for a request.
`void`
`bind(ServletRequest request)`
Bind the parameters of the given request to this binder's target, also binding multipart files in case of a multipart request.
`void`
`closeNoCatch()`
Treats errors as fatal.
`void`
`construct(ServletRequest request)`
Use a default or single data constructor to create the target by binding request parameters, multipart files, or parts to constructor args.
`protected ServletRequestDataBinder.ServletRequestValueResolver`
`createValueResolver(ServletRequest request)`
Allow subclasses to create the `DataBinder.ValueResolver` instance to use.
`protected boolean`
`shouldConstructArgument(org.springframework.core.MethodParameter param)`
`static org.springframework.validation.DataBinder.ValueResolver`
`valueResolver(ServletRequest request, WebDataBinder binder)`
Return a `ServletRequest` `DataBinder.ValueResolver`.

### Methods inherited from class org.springframework.web.bind.WebDataBinder

`adaptEmptyArrayIndices, bindMultipart, checkFieldDefaults, checkFieldMarkers, doBind, getEmptyValue, getEmptyValue, getFieldDefaultPrefix, getFieldMarkerPrefix, isBindEmptyMultipartFiles, resolvePrefixValue, setBindEmptyMultipartFiles, setFieldDefaultPrefix, setFieldMarkerPrefix`

### Methods inherited from class org.springframework.validation.DataBinder

`addCustomFormatter, addCustomFormatter, addCustomFormatter, addValidators, applyPropertyValues, bind, checkAllowedFields, checkRequiredFields, close, construct, convertIfNecessary, convertIfNecessary, convertIfNecessary, convertIfNecessary, createBeanPropertyBindingResult, createDirectFieldBindingResult, findCustomEditor, getAllowedFields, getAutoGrowCollectionLimit, getBindingErrorProcessor, getBindingResult, getConversionService, getDisallowedFields, getInternalBindingResult, getNameResolver, getObjectName, getPropertyAccessor, getPropertyEditorRegistry, getRequiredFields, getSimpleTypeConverter, getTarget, getTargetType, getTypeConverter, getValidator, getValidators, getValidatorsToApply, initBeanPropertyAccess, initDirectFieldAccess, isAllowed, isAutoGrowNestedPaths, isDeclarativeBinding, isIgnoreInvalidFields, isIgnoreUnknownFields, registerCustomEditor, registerCustomEditor, replaceValidators, setAllowedFields, setAutoGrowCollectionLimit, setAutoGrowNestedPaths, setBindingErrorProcessor, setConversionService, setDeclarativeBinding, setDisallowedFields, setExcludedValidators, setIgnoreInvalidFields, setIgnoreUnknownFields, setMessageCodesResolver, setNameResolver, setRequiredFields, setTargetType, setValidator, shouldNotBindPropertyValues, validate, validate`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ServletRequestDataBinder

public ServletRequestDataBinder(@Nullable Object target)
Create a new ServletRequestDataBinder instance, with default object name.

Parameters:
  `target` - the target object to bind onto (or `null` if the binder is just used to convert a plain parameter value)
See Also:
  - `DataBinder.DEFAULT_OBJECT_NAME`

### ServletRequestDataBinder

public ServletRequestDataBinder(@Nullable Object target, String objectName)
Create a new ServletRequestDataBinder instance.

Parameters:
  `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 object

## Method Details

### construct

public void construct(ServletRequest request)
Use a default or single data constructor to create the target by binding request parameters, multipart files, or parts to constructor args.

After the call, use `DataBinder.getBindingResult()` to check for bind errors. If there are none, the target is set, and `bind(ServletRequest)` can be called for further initialization via setters.

Parameters:
  `request` - the request to bind
Since:
  6.1

### createValueResolver

protected ServletRequestDataBinder.ServletRequestValueResolver createValueResolver(ServletRequest request)
Allow subclasses to create the `DataBinder.ValueResolver` instance to use.

Since:
  6.1

### shouldConstructArgument

protected boolean shouldConstructArgument(org.springframework.core.MethodParameter param)

Overrides:
  `shouldConstructArgument` in class `org.springframework.validation.DataBinder`

### bind

public void bind(ServletRequest request)
Bind the parameters of the given request to this binder's target, also binding multipart files in case of a multipart request.

This call can create field errors, representing basic binding errors like a required field (code "required"), or type mismatch between value and bean property (code "typeMismatch").

Multipart files are bound via their parameter name, just like normal HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property, invoking a "setUploadedFile" setter method.

The type of the target property for a multipart file can be MultipartFile, byte[], or String. Servlet Part binding is also supported when the request has not been parsed to MultipartRequest via MultipartResolver.

Parameters:
  `request` - the request with parameters to bind (can be multipart)
See Also:
  - `MultipartHttpServletRequest`
  - `MultipartRequest`
  - `MultipartFile`
  - `Part`
  - `DataBinder.bind(org.springframework.beans.PropertyValues)`

### addBindValues

protected void addBindValues(org.springframework.beans.MutablePropertyValues mpvs, ServletRequest request)
Extension point that subclasses can use to add extra bind values for a request. Invoked before `WebDataBinder.doBind(MutablePropertyValues)`. The default implementation is empty.

Parameters:
  `mpvs` - the property values that will be used for data binding
  `request` - the current request

### closeNoCatch

public void closeNoCatch() throws ServletRequestBindingException
Treats errors as fatal.

Use this method only if it's an error if the input isn't valid. This might be appropriate if all input is from dropdowns, for example.

Throws:
  `ServletRequestBindingException` - subclass of ServletException on any binding problem

### valueResolver

public static org.springframework.validation.DataBinder.ValueResolver valueResolver(ServletRequest request, WebDataBinder binder)
Return a `ServletRequest` `DataBinder.ValueResolver`. Mainly for use from `WebRequestDataBinder`.

Since:
  6.1