类 WebDataBinder

    • 字段详细资料

      • 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, that is, existed in the form that caused the submission. If no corresponding field value parameter was found, the field will be reset. The value of the field marker parameter does not matter in this case; an arbitrary value can be used. This is particularly useful for HTML checkboxes and select options.

        另请参阅:
        setFieldMarkerPrefix(java.lang.String), 常量字段值
    • 构造器详细资料

      • WebDataBinder

        public WebDataBinder​(Object target)
        Create a new WebDataBinder instance, with default object name.
        参数:
        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
      • WebDataBinder

        public WebDataBinder​(Object target,
                             String objectName)
        Create a new WebDataBinder instance.
        参数:
        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 object
    • 方法详细资料

      • 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.

        另请参阅:
        DEFAULT_FIELD_MARKER_PREFIX
      • setFieldDefaultPrefix

        public void setFieldDefaultPrefix​(String fieldDefaultPrefix)
        Specify a prefix that can be used for parameters that indicate default value fields, having "prefix + field" as name. The value of the default field is used when the field is not provided.

        Default is "!", for "!FIELD" parameters (e.g. "!subscribeToNewsletter"). Set this to null if you want to turn off the field defaults 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. A default field is especially useful when a checkbox represents a non-boolean value.

        The presence of a default parameter preempts the behavior of a field marker for the given field.

        另请参阅:
        DEFAULT_FIELD_DEFAULT_PREFIX
      • 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.

        另请参阅:
        MultipartFile
      • checkFieldDefaults

        protected void checkFieldDefaults​(MutablePropertyValues mpvs)
        Check the given property values for field defaults, i.e. for fields that start with the field default prefix.

        The existence of a field defaults indicates that the specified value should be used if the field is otherwise not present.

        参数:
        mpvs - the property values to be bound (can be modified)
        另请参阅:
        getFieldDefaultPrefix()
      • checkFieldMarkers

        protected void checkFieldMarkers​(MutablePropertyValues mpvs)
        Check the given property values for field markers, i.e. for fields that start with the field marker prefix.

        The existence of a field marker indicates that the specified field existed in the form. If the property values do not contain a corresponding field value, the field will be considered as empty and will be reset appropriately.

        参数:
        mpvs - the property values to be bound (can be modified)
        另请参阅:
        getFieldMarkerPrefix(), getEmptyValue(String, Class)
      • getEmptyValue

        protected Object getEmptyValue​(String field,
                                       Class<?> fieldType)
        Determine an empty value for the specified field.

        The default implementation returns:

        • Boolean.FALSE for boolean fields
        • an empty array for array types
        • Collection implementations for Collection types
        • Map implementations for Map types
        • else, null is used as default
        参数:
        field - the name of the field
        fieldType - the type of the field
        返回:
        the empty value (for most fields: null)
      • bindMultipart

        protected void bindMultipart​(Map<String,​List<MultipartFile>> multipartFiles,
                                     MutablePropertyValues mpvs)
        Bind all multipart files contained in the given request, if any (in case of a multipart request). To be called by subclasses.

        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.

        参数:
        multipartFiles - Map of field name String to MultipartFile object
        mpvs - the property values to be bound (can be modified)
        另请参阅:
        MultipartFile, setBindEmptyMultipartFiles(boolean)