📄 databinder.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.validation;
import java.beans.PropertyEditor;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MethodInvocationException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessException;
import org.springframework.beans.PropertyAccessExceptionsException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.TypeMismatchException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.util.StringUtils;
/**
* Binder that allows for binding property values to a target object.
* The binding process can be customized through specifying allowed fields,
* required fields, and custom editors.
*
* <p>Note that there are potential security implications in failing to set
* an array of allowed fields. In the case of HTTP form POST data for example,
* malicious clients can attempt to subvert an application by supplying values
* for fields or properties that do not exist on the form. In some cases this
* could lead to illegal data being set on command objects <i>or their nested
* objects</i>. For this reason, it is <b>highly recommended to specify the
* {@link #setAllowedFields allowedFields} property</b> on the DataBinder.
*
* <p>The binding results can be examined via the Errors interface,
* available as BindException instance. Missing field errors and property
* access exceptions will be converted to FieldErrors, collected in the
* Errors instance, with the following error codes:
*
* <ul>
* <li>Missing field error: "required"
* <li>Type mismatch error: "typeMismatch"
* <li>Method invocation error: "methodInvocation"
* </ul>
*
* <p>Custom validation errors can be added afterwards. You will typically
* want to resolve such error codes into proper user-visible error messages;
* this can be achieved through resolving each error via a MessageSource.
* The list of message codes to try can be customized through the
* MessageCodesResolver strategy. DefaultMessageCodesResolver's javadoc
* gives details on the default resolution rules.
*
* <p>This generic data binder can be used in any sort of environment.
* It is heavily used by Spring's web binding features, via the subclass
* ServletRequestDataBinder.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see #setAllowedFields
* @see #setRequiredFields
* @see #registerCustomEditor
* @see #setMessageCodesResolver
* @see #bind
* @see #getErrors
* @see DefaultMessageCodesResolver
* @see org.springframework.context.MessageSource
* @see org.springframework.web.bind.ServletRequestDataBinder
*/
public class DataBinder {
/**
* Error code that a missing field error (i.e. a required field not
* found in the list of property values) will be registered with:
* "required".
*/
public static final String MISSING_FIELD_ERROR_CODE = "required";
/**
* Error code that a type mismatch error (i.e. a property value not
* matching the type of the target field) will be registered with:
* "typeMismatch".
* @see org.springframework.beans.TypeMismatchException#ERROR_CODE
*/
public static final String TYPE_MISMATCH_ERROR_CODE = TypeMismatchException.ERROR_CODE;
/**
* Error code that a type mismatch error (i.e. a property value not
* matching the type of the target field) will be registered with:
* "methodInvocation".
* @see org.springframework.beans.MethodInvocationException#ERROR_CODE
*/
public static final String METHOD_INVOCATION_ERROR_CODE = MethodInvocationException.ERROR_CODE;
/**
* We'll create a lot of DataBinder instances: Let's use a static logger.
*/
protected static final Log logger = LogFactory.getLog(DataBinder.class);
private final BindException errors;
private boolean ignoreUnknownFields = true;
private String[] allowedFields;
private String[] requiredFields;
/**
* Create a new DataBinder instance.
* @param target target object to bind onto
* @param objectName name of the target object
*/
public DataBinder(Object target, String objectName) {
this.errors = createErrors(target, objectName);
}
/**
* Create a new Errors instance for this data binder.
* Can be overridden in subclasses to.
* Needs to be a subclass of BindException.
* @param target target object to bind onto
* @param objectName name of the target object
* @return the Errors instance
* @see #close
*/
protected BindException createErrors(Object target, String objectName) {
return new BindException(target, objectName);
}
/**
* Return the wrapped target object.
*/
public Object getTarget() {
return this.errors.getTarget();
}
/**
* Return the name of the bound object.
*/
public String getObjectName() {
return this.errors.getObjectName();
}
/**
* Return the Errors instance for this data binder.
* @return the Errors instance, to be treated as Errors or as BindException
* @see Errors
*/
public BindException getErrors() {
return errors;
}
/**
* Return the underlying BeanWrapper of the Errors object.
* To be used by binder subclasses that need bean property checks.
*/
protected BeanWrapper getBeanWrapper() {
return this.errors.getBeanWrapper();
}
/**
* Set whether to ignore unknown fields, i.e. whether to ignore request
* parameters that don't have corresponding fields in the target object.
*/
public void setIgnoreUnknownFields(boolean ignoreUnknownFields) {
this.ignoreUnknownFields = ignoreUnknownFields;
}
/**
* Return whether to ignore unknown fields, i.e. whether to ignore request
* parameters that don't have corresponding fields in the target object.
*/
public boolean isIgnoreUnknownFields() {
return ignoreUnknownFields;
}
/**
* Register fields that should be allowed for binding. Default is all
* fields. Restrict this for example to avoid unwanted modifications
* by malicious users when binding HTTP request parameters.
* <p>Supports "xxx*" and "*xxx" patterns. More sophisticated matching
* can be implemented by overriding the isAllowed method.
* @param allowedFields array of field names
* @see org.springframework.web.bind.ServletRequestDataBinder
* @see #isAllowed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -