revised DefaultBindingErrorProcessor to use direct getPropertyName() and getValue() calls on PropertyAccessException itself (SPR-6111)
This commit is contained in:
parent
9f19e5e73b
commit
34357d2b1f
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2007 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -55,11 +55,25 @@ public abstract class PropertyAccessException extends BeansException implements
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the PropertyChangeEvent that resulted in the problem.
|
* Return the PropertyChangeEvent that resulted in the problem.
|
||||||
* May be <code>null</code>; only available if an actual bean property
|
* <p>May be <code>null</code>; only available if an actual bean property
|
||||||
* was affected.
|
* was affected.
|
||||||
*/
|
*/
|
||||||
public PropertyChangeEvent getPropertyChangeEvent() {
|
public PropertyChangeEvent getPropertyChangeEvent() {
|
||||||
return this.propertyChangeEvent;
|
return this.propertyChangeEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the affected property, if available.
|
||||||
|
*/
|
||||||
|
public String getPropertyName() {
|
||||||
|
return (this.propertyChangeEvent != null ? this.propertyChangeEvent.getPropertyName() : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the affected value that was about to be set, if any.
|
||||||
|
*/
|
||||||
|
public Object getValue() {
|
||||||
|
return (this.propertyChangeEvent != null ? this.propertyChangeEvent.getNewValue() : null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2008 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -20,6 +20,7 @@ import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Combined exception, composed of individual PropertyAccessException instances.
|
* Combined exception, composed of individual PropertyAccessException instances.
|
||||||
|
|
@ -60,19 +61,18 @@ public class PropertyBatchUpdateException extends BeansException {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an array of the propertyAccessExceptions stored in this object.
|
* Return an array of the propertyAccessExceptions stored in this object.
|
||||||
* Will return the empty array (not null) if there were no errors.
|
* <p>Will return the empty array (not <code>null</code>) if there were no errors.
|
||||||
*/
|
*/
|
||||||
public final PropertyAccessException[] getPropertyAccessExceptions() {
|
public final PropertyAccessException[] getPropertyAccessExceptions() {
|
||||||
return this.propertyAccessExceptions;
|
return this.propertyAccessExceptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the exception for this field, or <code>null</code> if there isn't one.
|
* Return the exception for this field, or <code>null</code> if there isn't any.
|
||||||
*/
|
*/
|
||||||
public PropertyAccessException getPropertyAccessException(String propertyName) {
|
public PropertyAccessException getPropertyAccessException(String propertyName) {
|
||||||
for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
|
for (PropertyAccessException pae : this.propertyAccessExceptions) {
|
||||||
PropertyAccessException pae = this.propertyAccessExceptions[i];
|
if (ObjectUtils.nullSafeEquals(propertyName, pae.getPropertyName())) {
|
||||||
if (propertyName.equals(pae.getPropertyChangeEvent().getPropertyName())) {
|
|
||||||
return pae;
|
return pae;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2008 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -94,6 +94,7 @@ public class TypeMismatchException extends PropertyAccessException {
|
||||||
/**
|
/**
|
||||||
* Return the offending value (may be <code>null</code>)
|
* Return the offending value (may be <code>null</code>)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2008 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -62,12 +62,11 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||||
|
|
||||||
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
|
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
|
||||||
// Create field error with the exceptions's code, e.g. "typeMismatch".
|
// Create field error with the exceptions's code, e.g. "typeMismatch".
|
||||||
String field = ex.getPropertyChangeEvent().getPropertyName();
|
String field = ex.getPropertyName();
|
||||||
Object value = ex.getPropertyChangeEvent().getNewValue();
|
|
||||||
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
|
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
|
||||||
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
|
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
|
||||||
bindingResult.addError(new FieldError(
|
bindingResult.addError(new FieldError(
|
||||||
bindingResult.getObjectName(), field, value, true,
|
bindingResult.getObjectName(), field, ex.getValue(), true,
|
||||||
codes, arguments, ex.getLocalizedMessage()));
|
codes, arguments, ex.getLocalizedMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,8 +82,7 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||||
*/
|
*/
|
||||||
protected Object[] getArgumentsForBindError(String objectName, String field) {
|
protected Object[] getArgumentsForBindError(String objectName, String field) {
|
||||||
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
|
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
|
||||||
String defaultMessage = field;
|
return new Object[] {new DefaultMessageSourceResolvable(codes, field)};
|
||||||
return new Object[] {new DefaultMessageSourceResolvable(codes, defaultMessage)};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue