public class WebRequestDataBinder extends WebDataBinder
DataBinder
to perform data binding
from web request parameters to JavaBeans, including support for multipart files.
See the DataBinder/WebDataBinder superclasses for customization options, which include specifying allowed/required fields, and registering custom property editors.
Can also used for manual data binding in custom web controllers or interceptors
that build on Spring's WebRequest
abstraction: e.g. in a WebRequestInterceptor
implementation. Simply instantiate a WebRequestDataBinder for each binding
process, and invoke bind
with the current WebRequest as argument:
MyBean myBean = new MyBean(); // apply binder to custom target object WebRequestDataBinder binder = new WebRequestDataBinder(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(); ...
bind(org.springframework.web.context.request.WebRequest)
,
DataBinder.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)
,
DataBinder.setAllowedFields(java.lang.String...)
,
DataBinder.setRequiredFields(java.lang.String...)
,
WebDataBinder.setFieldMarkerPrefix(java.lang.String)
Modifier and Type | Class and Description |
---|---|
private static class |
WebRequestDataBinder.Servlet3MultipartHelper
Encapsulate Part binding code for Servlet 3.0+ only containers.
|
DEFAULT_FIELD_DEFAULT_PREFIX, DEFAULT_FIELD_MARKER_PREFIX
DEFAULT_AUTO_GROW_COLLECTION_LIMIT, DEFAULT_OBJECT_NAME, logger
Constructor and Description |
---|
WebRequestDataBinder(java.lang.Object target)
Create a new WebRequestDataBinder instance, with default object name.
|
WebRequestDataBinder(java.lang.Object target,
java.lang.String objectName)
Create a new WebRequestDataBinder instance.
|
Modifier and Type | Method and Description |
---|---|
void |
bind(WebRequest 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.
|
private boolean |
isMultipartRequest(WebRequest request)
Check if the request is a multipart request (by checking its Content-Type header).
|
bindMultipart, checkFieldDefaults, checkFieldMarkers, doBind, getEmptyValue, getFieldDefaultPrefix, getFieldMarkerPrefix, isBindEmptyMultipartFiles, setBindEmptyMultipartFiles, setFieldDefaultPrefix, setFieldMarkerPrefix
addCustomFormatter, addCustomFormatter, addCustomFormatter, addValidators, applyPropertyValues, bind, checkAllowedFields, checkRequiredFields, close, convertIfNecessary, convertIfNecessary, convertIfNecessary, 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, setExtractOldValueForEditor, setIgnoreInvalidFields, setIgnoreUnknownFields, setMessageCodesResolver, setRequiredFields, setValidator, validate, validate
public WebRequestDataBinder(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 WebRequestDataBinder(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 bind(WebRequest 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 Part, MultipartFile, byte[], or String. The latter two receive the contents of the uploaded file; all metadata like original file name, content type, etc are lost in those cases.
request
- request with parameters to bind (can be multipart)MultipartRequest
,
MultipartFile
,
javax.servlet.http.Part
,
DataBinder.bind(org.springframework.beans.PropertyValues)
private boolean isMultipartRequest(WebRequest request)
request
- request with parameters to bindpublic void closeNoCatch() throws BindException
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.
BindException
- if binding errors have been encountered