|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.validation.DataBinder org.springframework.web.bind.ServletRequestDataBinder
Special binder to perform data binding from servlet request parameters to JavaBeans, including support for multipart files.
See the DataBinder superclass 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.
Simply instantiate a ServletRequestDataBinder for each binding process,
and invoke bind
with the current ServletRequest as argument.
bind(javax.servlet.ServletRequest)
,
DataBinder.setAllowedFields(java.lang.String[])
,
DataBinder.setRequiredFields(java.lang.String[])
,
DataBinder.registerCustomEditor(java.lang.Class, java.beans.PropertyEditor)
,
BaseCommandController.initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
,
MultiActionController
Field Summary | |
static String |
DEFAULT_FIELD_MARKER_PREFIX
Default prefix that field marker parameters start with, followed by the field name: e.g. |
Fields inherited from class org.springframework.validation.DataBinder |
logger, METHOD_INVOCATION_ERROR_CODE, MISSING_FIELD_ERROR_CODE, TYPE_MISMATCH_ERROR_CODE |
Constructor Summary | |
ServletRequestDataBinder(Object target,
String objectName)
Create a new DataBinder 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. |
void |
closeNoCatch()
Treats errors as fatal. |
void |
setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)
Set whether to bind empty MultipartFile parameters. |
void |
setFieldMarkerPrefix(String fieldMarkerPrefix)
Specify a prefix that can be used for parameters that mark potentially empty fields, having "prefix + field" as name. |
Methods inherited from class org.springframework.validation.DataBinder |
bind, close, createErrors, getAllowedFields, getArgumentsForBindingError, getBeanWrapper, getErrors, getObjectName, getRequiredFields, getTarget, isAllowed, isIgnoreUnknownFields, registerCustomEditor, registerCustomEditor, setAllowedFields, setIgnoreUnknownFields, setMessageCodesResolver, setRequiredFields |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String DEFAULT_FIELD_MARKER_PREFIX
Such a marker parameter indicates that the field was visible respectively existed in the form that caused the submission. If no corresponding field value parameter was found, the field will be reset. This is particularly useful for HTML checkboxes and select options.
setFieldMarkerPrefix(java.lang.String)
,
Constant Field ValuesConstructor Detail |
public ServletRequestDataBinder(Object target, String objectName)
target
- target object to bind ontoobjectName
- objectName of the target objectMethod Detail |
public void setFieldMarkerPrefix(String fieldMarkerPrefix)
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.
DEFAULT_FIELD_MARKER_PREFIX
,
BaseCommandController.onBind(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
public void setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles)
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.
public void bind(ServletRequest 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.
request
- request with parameters to bind (can be multipart)MultipartHttpServletRequest
,
MultipartFile
public void closeNoCatch() throws ServletRequestBindingException
ServletRequestBindingException
- subclass of ServletException on any binding problem
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |