org.springframework.web.bind
Class ServletRequestDataBinder

java.lang.Object
  extended byorg.springframework.validation.DataBinder
      extended byorg.springframework.web.bind.ServletRequestDataBinder

public class ServletRequestDataBinder
extends DataBinder

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.

Author:
Rod Johnson, Juergen Hoeller
See Also:
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

DEFAULT_FIELD_MARKER_PREFIX

public static final String DEFAULT_FIELD_MARKER_PREFIX
Default prefix that field marker parameters start with, followed by the field name: e.g. "_subscribeToNewsletter" for a field "subscribeToNewsletter".

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.

See Also:
setFieldMarkerPrefix(java.lang.String), Constant Field Values
Constructor Detail

ServletRequestDataBinder

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

Parameters:
target - target object to bind onto
objectName - objectName of the target object
Method Detail

setFieldMarkerPrefix

public void setFieldMarkerPrefix(String fieldMarkerPrefix)
Specify a prefix that can be used for parameters that mark potentially empty fields, having "prefix + field" as name. Such a marker parameter is checked by existence: You can send any value for it, for example "visible". This is particularly useful for HTML checkboxes and select options.

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.

See Also:
DEFAULT_FIELD_MARKER_PREFIX, BaseCommandController.onBind(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)

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.


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:
request - request with parameters to bind (can be multipart)
See Also:
MultipartHttpServletRequest, MultipartFile

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


Copyright (C) 2003-2004 The Spring Framework Project.