📄 databinder.java
字号:
* @param requiredFields array of field names
* @see #setBindingErrorProcessor
* @see DefaultBindingErrorProcessor#MISSING_FIELD_ERROR_CODE
*/
public void setRequiredFields(String[] requiredFields) {
this.requiredFields = requiredFields;
if (logger.isDebugEnabled()) {
logger.debug("DataBinder requires binding of required fields [" +
StringUtils.arrayToCommaDelimitedString(requiredFields) + "]");
}
}
/**
* Return the fields that are required for each binding process.
* @return array of field names
*/
public String[] getRequiredFields() {
return requiredFields;
}
/**
* Set whether to extract the old field value when applying a
* property editor to a new value for a field.
* <p>Default is "true", exposing previous field values to custom editors.
* Turn this to "false" to avoid side effects caused by getters.
*/
public void setExtractOldValueForEditor(boolean extractOldValueForEditor) {
this.errors.getBeanWrapper().setExtractOldValueForEditor(extractOldValueForEditor);
}
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
this.errors.getBeanWrapper().registerCustomEditor(requiredType, propertyEditor);
}
public void registerCustomEditor(Class requiredType, String field, PropertyEditor propertyEditor) {
getBeanWrapper().registerCustomEditor(requiredType, field, propertyEditor);
}
public PropertyEditor findCustomEditor(Class requiredType, String propertyPath) {
return getBeanWrapper().findCustomEditor(requiredType, propertyPath);
}
/**
* Set the strategy to use for resolving errors into message codes.
* Applies the given strategy to the underlying errors holder.
* <p>Default is a DefaultMessageCodesResolver.
* @see BindException#setMessageCodesResolver
* @see DefaultMessageCodesResolver
*/
public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) {
this.errors.setMessageCodesResolver(messageCodesResolver);
}
/**
* Set the strategy to use for processing binding errors, that is,
* required field errors and <code>PropertyAccessException</code>s.
* <p>Default is a DefaultBindingErrorProcessor.
* @see DefaultBindingErrorProcessor
*/
public void setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor) {
this.bindingErrorProcessor = bindingErrorProcessor;
}
/**
* Return the strategy for processing binding errors.
*/
public BindingErrorProcessor getBindingErrorProcessor() {
return bindingErrorProcessor;
}
/**
* Bind the given property values to this binder's target.
* <p>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").
* <p>Note that the given PropertyValues should be a throwaway instance:
* For efficiency, it will be modified to just contain allowed fields if it
* implements the MutablePropertyValues interface; else, an internal mutable
* copy will be created for this purpose. Pass in a copy of the PropertyValues
* if you want your original instance to stay unmodified in any case.
* @param pvs property values to bind
* @see #doBind(org.springframework.beans.MutablePropertyValues)
*/
public void bind(PropertyValues pvs) {
MutablePropertyValues mpvs = (pvs instanceof MutablePropertyValues) ?
(MutablePropertyValues) pvs : new MutablePropertyValues(pvs);
doBind(mpvs);
}
/**
* Actual implementation of the binding process, working with the
* passed-in MutablePropertyValues instance.
* @param mpvs the property values to bind,
* as MutablePropertyValues instance
* @see #checkAllowedFields
* @see #checkRequiredFields
* @see #applyPropertyValues
*/
protected void doBind(MutablePropertyValues mpvs) {
checkAllowedFields(mpvs);
checkRequiredFields(mpvs);
applyPropertyValues(mpvs);
}
/**
* Check the given property values against the allowed fields,
* removing values for fields that are not allowed.
* @param mpvs the property values to be bound (can be modified)
* @see #getAllowedFields
* @see #isAllowed(String)
*/
protected void checkAllowedFields(MutablePropertyValues mpvs) {
List allowedFieldsList = (getAllowedFields() != null) ? Arrays.asList(getAllowedFields()) : null;
PropertyValue[] pvArray = mpvs.getPropertyValues();
for (int i = 0; i < pvArray.length; i++) {
String field = pvArray[i].getName();
if (!((allowedFieldsList != null && allowedFieldsList.contains(field)) || isAllowed(field))) {
mpvs.removePropertyValue(pvArray[i]);
if (logger.isDebugEnabled()) {
logger.debug("Field [" + pvArray[i] + "] has been removed from PropertyValues " +
"and will not be bound, because it has not been found in the list of allowed fields " +
allowedFieldsList);
}
}
}
}
/**
* Return if the given field is allowed for binding.
* Invoked for each passed-in property value.
* <p>The default implementation checks for "xxx*" and "*xxx" matches.
* Can be overridden in subclasses.
* <p>If the field is found in the allowedFields array as direct match,
* this method will not be invoked.
* @param field the field to check
* @return if the field is allowed
* @see #setAllowedFields
*/
protected boolean isAllowed(String field) {
if (getAllowedFields() != null) {
String[] allowedFields = getAllowedFields();
for (int i = 0; i < allowedFields.length; i++) {
String allowed = allowedFields[i];
if ((allowed.endsWith("*") && field.startsWith(allowed.substring(0, allowed.length() - 1))) ||
(allowed.startsWith("*") && field.endsWith(allowed.substring(1, allowed.length())))) {
return true;
}
}
return false;
}
return true;
}
/**
* Check the given property values against the required fields,
* generating missing field errors where appropriate.
* @param mpvs the property values to be bound (can be modified)
* @see #getRequiredFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processMissingFieldError
*/
protected void checkRequiredFields(MutablePropertyValues mpvs) {
if (getRequiredFields() != null) {
String[] requiredFields = getRequiredFields();
for (int i = 0; i < requiredFields.length; i++) {
PropertyValue pv = mpvs.getPropertyValue(requiredFields[i]);
if (pv == null || pv.getValue() == null ||
(pv.getValue() instanceof String && !StringUtils.hasText((String) pv.getValue()))) {
// Use bind error processor to create FieldError.
String field = requiredFields[i];
getBindingErrorProcessor().processMissingFieldError(field, getErrors());
// Remove property from property values to bind:
// It has already caused a field error with a rejected value.
mpvs.removePropertyValue(field);
}
}
}
}
/**
* Apply given property values to the target object.
* <p>Default implementation applies them all of them as bean property
* values via the corresponding BeanWrapper. Unknown fields will by
* default be ignored.
* @param mpvs the property values to be bound (can be modified)
* @see #getTarget
* @see #getBeanWrapper
* @see #isIgnoreUnknownFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processPropertyAccessException
*/
protected void applyPropertyValues(MutablePropertyValues mpvs) {
try {
// Bind request parameters onto target object.
getBeanWrapper().setPropertyValues(mpvs, isIgnoreUnknownFields());
}
catch (PropertyAccessExceptionsException ex) {
// Use bind error processor to create FieldErrors.
PropertyAccessException[] exs = ex.getPropertyAccessExceptions();
for (int i = 0; i < exs.length; i++) {
getBindingErrorProcessor().processPropertyAccessException(exs[i], getErrors());
}
}
}
/**
* Close this DataBinder, which may result in throwing
* a BindException if it encountered any errors
* @return the model Map, containing target object and Errors instance
* @throws BindException if there were any errors in the bind operation
* @see BindException#getModel
*/
public Map close() throws BindException {
if (getErrors().hasErrors()) {
throw getErrors();
}
return getErrors().getModel();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -