org.springframework.web.bind
Class ServletRequestDataBinder

public class ServletRequestDataBinder
extends WebDataBinder
Special DataBinder to perform data binding from servlet 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.

Used by Spring web MVC's BaseCommandController and MultiActionController. Note that BaseCommandController and its subclasses allow for easy customization of the binder instances that they use through overriding initBinder.

Can also be used for manual data binding in custom web controllers: for example, in a plain Controller implementation or in a MultiActionController handler method. 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();
 ...
SinceNot specified.
VersionNot specified.
AuthorRod Johnson, Juergen Hoeller
Wiki javadoc Use textile entry format.
Add your comments here.
Fields inherited from org.springframework.validationDataBinder
Fields inherited from org.springframework.web.bindWebDataBinder
Constructor Summary
ServletRequestDataBinder( Object target )
Create a new ServletRequestDataBinder instance, with default object name.
ServletRequestDataBinder( Object target, String objectName )
Create a new ServletRequestDataBinder instance.
Method Summary
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.
protected void checkMultipartFiles( ServletRequest request, MutablePropertyValues mpvs )
Check the multipart files contained in the given request, if any (in case of a multipart request).
void closeNoCatch()
Treats errors as fatal.
boolean isBindEmptyMultipartFiles()
Return whether to bind empty MultipartFile parameters.
void setBindEmptyMultipartFiles( boolean bindEmptyMultipartFiles )
Set whether to bind empty MultipartFile parameters.
ServletRequestDataBinder
public ServletRequestDataBinder ( Object target )
Create a new ServletRequestDataBinder instance, with default object name.
Parameters
TypeNameDescription
Object target target object to bind onto
Wiki javadoc Use textile entry format.
Add your comments here.
ServletRequestDataBinder
public ServletRequestDataBinder ( Object target, String objectName )
Create a new ServletRequestDataBinder instance.
Parameters
TypeNameDescription
Object target target object to bind onto
String objectName objectName of the target object
Wiki javadoc Use textile entry format.
Add your comments here.
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. The latter two receive the contents of the uploaded file; all metadata like original file name, content type, etc are lost in those cases.

Parameters
TypeNameDescription
ServletRequest request request with parameters to bind (can be multipart)
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
checkMultipartFiles
protected void checkMultipartFiles ( ServletRequest request, MutablePropertyValues mpvs )
Check the multipart files contained in the given request, if any (in case of a multipart request).

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
TypeNameDescription
ServletRequest request current request (can be multipart)
MutablePropertyValues mpvs the property values to be bound (can be modified)
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
closeNoCatch
public void closeNoCatch ( )
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.
Returns void No description provided.
Exceptions
ServletRequestBindingException subclass of ServletException on any binding problem
Wiki javadoc Use textile entry format.
Add your comments here.
isBindEmptyMultipartFiles
public boolean isBindEmptyMultipartFiles ( )
Return whether to bind empty MultipartFile parameters.
Wiki javadoc Use textile entry format.
Add your comments here.
setBindEmptyMultipartFiles
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.

Parameters
TypeNameDescription
boolean bindEmptyMultipartFiles No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.