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

public Class ServletRequestDataBinder

extends WebDataBinder
Class Inheritance
Imports
java.lang.reflect.Array, java.util.Enumeration, .LinkedHashSet, .List, .Set, jakarta.servlet.ServletRequest, jakarta.servlet.http.HttpServletRequest, .Part, org.springframework.beans.MutablePropertyValues, org.springframework.core.MethodParameter, org.springframework.http.HttpMethod, .MediaType, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils, org.springframework.validation.BindException, org.springframework.web.multipart.MultipartFile, .MultipartRequest, org.springframework.web.multipart.support.StandardServletPartUtils, org.springframework.web.util.WebUtils

Special org.springframework.validation.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
Errors errors = binder.getErrors();
...
Authors
Rod Johnson, Juergen Hoeller
See Also
bind(jakarta.servlet.ServletRequest), registerCustomEditor, setAllowedFields, setRequiredFields, setFieldMarkerPrefix

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
ServletRequestDataBinder.ServletRequestValueResolver

Resolver that looks up values to bind in a ServletRequest.

Field Summary

Inherited from org.springframework.web.bind.WebDataBinder:
DEFAULT_FIELD_DEFAULT_PREFIXDEFAULT_FIELD_MARKER_PREFIX

Constructor Summary

AccessConstructor and Description
public
ServletRequestDataBinder(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 ServletRequestDataBinder instance, with default object name.

public
ServletRequestDataBinder(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 ServletRequestDataBinder instance.

Method Summary

Modifier and TypeMethod and Description
protected void
addBindValues(MutablePropertyValues
the property values that will be used for data binding
mpvs
,
ServletRequest
the current request
request
)

Extension point that subclasses can use to add extra bind values for a request.

public void
bind(ServletRequest
the request with parameters to bind (can be multipart)
request
)

Bind the parameters of the given request to this binder's target, also binding multipart files in case of a multipart request.

public void
closeNoCatch()

Treats errors as fatal.

public void
construct(ServletRequest
the request to bind
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 ValueResolver instance to use.

private static boolean
protected boolean
shouldConstructArgument(MethodParameter param)

Overrides org.springframework.validation.DataBinder.shouldConstructArgument.

Whether to instantiate the constructor argument of the given type, matching its own constructor arguments to bind values.
public static DataBinder.ValueResolver
valueResolver(ServletRequest request, WebDataBinder binder)

Return a ServletRequest ValueResolver.

Inherited from org.springframework.web.bind.WebDataBinder:
adaptEmptyArrayIndicesbindMultipartcheckFieldDefaultscheckFieldMarkersdoBindgetEmptyValuegetEmptyValuegetFieldDefaultPrefixgetFieldMarkerPrefixisBindEmptyMultipartFilesresolvePrefixValuesetBindEmptyMultipartFilessetFieldDefaultPrefixsetFieldMarkerPrefix

Constructor Detail

ServletRequestDataBinderback to summary
public ServletRequestDataBinder(Object target)

Create a new ServletRequestDataBinder 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
ServletRequestDataBinderback to summary
public ServletRequestDataBinder(Object target, String objectName)

Create a new ServletRequestDataBinder 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

addBindValuesback to summary
protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request)

Extension point that subclasses can use to add extra bind values for a request. Invoked before doBind(MutablePropertyValues). The default implementation is empty.

Parameters
mpvs:MutablePropertyValues

the property values that will be used for data binding

request:ServletRequest

the current request

bindback to summary
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:ServletRequest

the request with parameters to bind (can be multipart)

See Also
org.springframework.web.multipart.MultipartHttpServletRequest, org.springframework.web.multipart.MultipartRequest, org.springframework.web.multipart.MultipartFile, jakarta.servlet.http.Part, bind(org.springframework.beans.PropertyValues)
closeNoCatchback to summary
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.

Exceptions
ServletRequestBindingException:
subclass of ServletException on any binding problem
constructback to summary
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 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:ServletRequest

the request to bind

Since
6.1
createValueResolverback to summary
protected ServletRequestDataBinder.ServletRequestValueResolver createValueResolver(ServletRequest request)

Allow subclasses to create the ValueResolver instance to use.

Since
6.1
isFormDataPostback to summary
private static boolean isFormDataPost(ServletRequest request)
shouldConstructArgumentback to summary
protected boolean shouldConstructArgument(MethodParameter param)

Overrides org.springframework.validation.DataBinder.shouldConstructArgument.

Doc from org.springframework.validation.DataBinder.shouldConstructArgument.

Whether to instantiate the constructor argument of the given type, matching its own constructor arguments to bind values.

By default, simple value types, maps, collections, and arrays are excluded from nested constructor binding initialization.

Annotations
@Override
valueResolverback to summary
public static DataBinder.ValueResolver valueResolver(ServletRequest request, WebDataBinder binder)

Return a ServletRequest ValueResolver. Mainly for use from org.springframework.web.bind.support.WebRequestDataBinder.

Since
6.1
org.springframework.web.bind back to summary

protected Class ServletRequestDataBinder.ServletRequestValueResolver

extends Object
implements ValueResolver
Class Inheritance
  • java.lang.Object
  • org.springframework.web.bind.ServletRequestDataBinder.ServletRequestValueResolver
All Implemented Interfaces
org.springframework.validation.DataBinder.ValueResolver

Resolver that looks up values to bind in a ServletRequest.

Field Summary

Modifier and TypeField and Description
private final WebDataBinder
private Set<String>
private final ServletRequest

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
private Object
public Set<String>
getNames()

Implements org.springframework.validation.DataBinder.ValueResolver.getNames.

Return the names of all property values.
protected ServletRequest
protected Object
protected Set<String>
public final Object
resolveValue(String
the name to use for the lookup, possibly a nested path for constructor parameters on nested objects
name
,
Class<?>
the target type, based on the constructor parameter type
paramType
)

Implements org.springframework.validation.DataBinder.ValueResolver.resolveValue.

Resolve the value for the given name and target parameter type.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataBinderback to summary
private final WebDataBinder dataBinder
parameterNamesback to summary
private Set<String> parameterNames
Annotations
@Nullable
requestback to summary
private final ServletRequest request

Constructor Detail

ServletRequestValueResolverback to summary
protected ServletRequestValueResolver(ServletRequest request, WebDataBinder dataBinder)

Method Detail

getMultipartValueback to summary
private Object getMultipartValue(String name)
Annotations
@Nullable
getNamesback to summary
public Set<String> getNames()

Implements org.springframework.validation.DataBinder.ValueResolver.getNames.

Doc from org.springframework.validation.DataBinder.ValueResolver.getNames.

Return the names of all property values.

Annotations
@Override
getRequestback to summary
protected ServletRequest getRequest()
getRequestParameterback to summary
protected Object getRequestParameter(String name, Class<?> type)
Annotations
@Nullable
initParameterNamesback to summary
protected Set<String> initParameterNames(ServletRequest request)
resolveValueback to summary
public final Object resolveValue(String name, Class<?> paramType)

Implements org.springframework.validation.DataBinder.ValueResolver.resolveValue.

Doc from org.springframework.validation.DataBinder.ValueResolver.resolveValue.

Resolve the value for the given name and target parameter type.

Parameters
name:String

the name to use for the lookup, possibly a nested path for constructor parameters on nested objects

paramType:Class<?>

the target type, based on the constructor parameter type

Returns:Object

the resolved value, possibly null if none found

Annotations
@Nullable
@Override