下面列出了org.springframework.beans.PropertyBatchUpdateException#org.springframework.beans.PropertyAccessException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
String field = ex.getPropertyName();
Assert.state(field != null, "No field in exception");
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue();
if (ObjectUtils.isArray(rejectedValue)) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage());
error.wrap(ex);
bindingResult.addError(error);
}
@Override
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
String field = ex.getPropertyName();
Assert.state(field != null, "No field in exception");
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue();
if (ObjectUtils.isArray(rejectedValue)) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage());
error.wrap(ex);
bindingResult.addError(error);
}
/**
* Adds an entry to the {@link org.kuali.rice.krad.util.GlobalVariables#getMessageMap()} for the given
* binding processing error
*
* @param ex exception that was thrown
* @param bindingResult binding result containing the results of the binding process
*/
@Override
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
super.processPropertyAccessException(ex, bindingResult);
Object rejectedValue = ex.getValue();
if (!(rejectedValue == null || rejectedValue.equals(""))) {
if (ex.getCause() instanceof FormatException) {
GlobalVariables.getMessageMap().putError(ex.getPropertyName(),
((FormatException) ex.getCause()).getErrorKey(),
new String[] {rejectedValue.toString()});
} else {
GlobalVariables.getMessageMap().putError(ex.getPropertyName(), RiceKeyConstants.ERROR_CUSTOM,
new String[] {"Invalid format"});
}
}
}
/**
* Apply given property values to the target object.
* <p>Default implementation applies all of the supplied property
* values as bean property values. By default, unknown fields will
* be ignored.
* @param mpvs the property values to be bound (can be modified)
* @see #getTarget
* @see #getPropertyAccessor
* @see #isIgnoreUnknownFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processPropertyAccessException
*/
protected void applyPropertyValues(MutablePropertyValues mpvs) {
try {
// Bind request parameters onto target object.
getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields());
}
catch (PropertyBatchUpdateException ex) {
// Use bind error processor to create FieldErrors.
for (PropertyAccessException pae : ex.getPropertyAccessExceptions()) {
getBindingErrorProcessor().processPropertyAccessException(pae, getInternalBindingResult());
}
}
}
/**
* Apply given property values to the target object.
* <p>Default implementation applies all of the supplied property
* values as bean property values. By default, unknown fields will
* be ignored.
* @param mpvs the property values to be bound (can be modified)
* @see #getTarget
* @see #getPropertyAccessor
* @see #isIgnoreUnknownFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processPropertyAccessException
*/
protected void applyPropertyValues(MutablePropertyValues mpvs) {
try {
// Bind request parameters onto target object.
getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields());
}
catch (PropertyBatchUpdateException ex) {
// Use bind error processor to create FieldErrors.
for (PropertyAccessException pae : ex.getPropertyAccessExceptions()) {
getBindingErrorProcessor().processPropertyAccessException(pae, getInternalBindingResult());
}
}
}
@Override
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
String field = ex.getPropertyName();
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue();
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
bindingResult.addError(new FieldError(
bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage()));
}
/**
* Apply given property values to the target object.
* <p>Default implementation applies all of the supplied property
* values as bean property values. By default, unknown fields will
* be ignored.
* @param mpvs the property values to be bound (can be modified)
* @see #getTarget
* @see #getPropertyAccessor
* @see #isIgnoreUnknownFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processPropertyAccessException
*/
protected void applyPropertyValues(MutablePropertyValues mpvs) {
try {
// Bind request parameters onto target object.
getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields());
}
catch (PropertyBatchUpdateException ex) {
// Use bind error processor to create FieldErrors.
for (PropertyAccessException pae : ex.getPropertyAccessExceptions()) {
getBindingErrorProcessor().processPropertyAccessException(pae, getInternalBindingResult());
}
}
}
@Override
public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
String field = ex.getPropertyName();
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue();
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
bindingResult.addError(new FieldError(
bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage()));
}
/**
* Apply given property values to the target object.
* <p>Default implementation applies all of the supplied property
* values as bean property values. By default, unknown fields will
* be ignored.
* @param mpvs the property values to be bound (can be modified)
* @see #getTarget
* @see #getPropertyAccessor
* @see #isIgnoreUnknownFields
* @see #getBindingErrorProcessor
* @see BindingErrorProcessor#processPropertyAccessException
*/
protected void applyPropertyValues(MutablePropertyValues mpvs) {
try {
// Bind request parameters onto target object.
getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields());
}
catch (PropertyBatchUpdateException ex) {
// Use bind error processor to create FieldErrors.
for (PropertyAccessException pae : ex.getPropertyAccessExceptions()) {
getBindingErrorProcessor().processPropertyAccessException(pae, getInternalBindingResult());
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void processPropertyAccessException(PropertyAccessException e, BindingResult bindingResult) {
TypeOrFormatMismatchException converted = null;
if (Controllers.exceptionMatch(e.getCause().getClass(), PropertyEditingException.class)) {
converted = new TypeOrFormatMismatchException(e, bindingResult);
super.processPropertyAccessException(converted, bindingResult);
} else {
super.processPropertyAccessException(e, bindingResult);
}
}
public void execute(ControlAccessor controlAccessor, String name) {
try {
modelPropertyAccessor.setPropertyValue(name, controlAccessor.getControlValue());
}
catch (PropertyAccessException pae) {
errorProcessor.processPropertyAccessException(pae, bindingResult);
}
}
/**
* Set value on binded object using the property name.
* @param value the value to set
*/
protected void setValue(Object value) {
BeanWrapper wrapper = getBeanWrapper();
Object convertedValue = convertIfNecessary(value, wrapper.getPropertyType(this.propertyName));
try {
wrapper.setPropertyValue(propertyName, convertedValue);
oldValue = value;
}
catch (PropertyAccessException pae) {
log.error(pae);
errorProcessor.processPropertyAccessException(component, pae, bindingResult);
}
}
/**
* Add a ControlError instead FieldError to hold component that has failed.
* @param control
* @param ex
* @param bindingResult
*/
public void processPropertyAccessException(Object control, PropertyAccessException ex,
BindingResult bindingResult ) {
// Create field error with the exceptions's code, e.g. "typeMismatch".
String field = ex.getPropertyName();
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue();
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
bindingResult.addError(new ControlError(control,
bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage()));
}
/**
* Set property, without trowing exceptions on errors
* @param bean bean name
* @param name name
* @param value value
*/
public static void setProperty(Object bean, String name, Object value) {
try {
BeanWrapper wrapper = new BeanWrapperImpl(bean);
wrapper.setPropertyValue(new PropertyValue(name, value));
} catch (InvalidPropertyException ipe) {
log.debug("Bean has no property: " + name);
} catch (PropertyAccessException pae) {
log.debug("Access Error on property: " + name);
}
}
protected static String generateMessage(PropertyAccessException e, BindingResult binding) {
PropertyEditingException ex = (PropertyEditingException) e.getCause();
return ex.getMessage();
}
/**
* Translate the given {@code PropertyAccessException} to an appropriate
* error registered on the given {@code Errors} instance.
* <p>Note that two error types are available: {@code FieldError} and
* {@code ObjectError}. Usually, field errors are created, but in certain
* situations one might want to create a global {@code ObjectError} instead.
* @param ex the {@code PropertyAccessException} to translate
* @param bindingResult the errors object to add the error(s) to.
* You can add more than just one error or maybe even ignore it.
* The {@code BindingResult} object features convenience utils such as
* a {@code resolveMessageCodes} method to resolve an error code.
* @see Errors
* @see FieldError
* @see ObjectError
* @see MessageCodesResolver
* @see BeanPropertyBindingResult#addError
* @see BeanPropertyBindingResult#resolveMessageCodes
*/
void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult);
/**
* Translate the given {@code PropertyAccessException} to an appropriate
* error registered on the given {@code Errors} instance.
* <p>Note that two error types are available: {@code FieldError} and
* {@code ObjectError}. Usually, field errors are created, but in certain
* situations one might want to create a global {@code ObjectError} instead.
* @param ex the {@code PropertyAccessException} to translate
* @param bindingResult the errors object to add the error(s) to.
* You can add more than just one error or maybe even ignore it.
* The {@code BindingResult} object features convenience utils such as
* a {@code resolveMessageCodes} method to resolve an error code.
* @see Errors
* @see FieldError
* @see ObjectError
* @see MessageCodesResolver
* @see BeanPropertyBindingResult#addError
* @see BeanPropertyBindingResult#resolveMessageCodes
*/
void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult);
/**
* Translate the given {@code PropertyAccessException} to an appropriate
* error registered on the given {@code Errors} instance.
* <p>Note that two error types are available: {@code FieldError} and
* {@code ObjectError}. Usually, field errors are created, but in certain
* situations one might want to create a global {@code ObjectError} instead.
* @param ex the {@code PropertyAccessException} to translate
* @param bindingResult the errors object to add the error(s) to.
* You can add more than just one error or maybe even ignore it.
* The {@code BindingResult} object features convenience utils such as
* a {@code resolveMessageCodes} method to resolve an error code.
* @see Errors
* @see FieldError
* @see ObjectError
* @see MessageCodesResolver
* @see BeanPropertyBindingResult#addError
* @see BeanPropertyBindingResult#resolveMessageCodes
*/
void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult);
/**
* Translate the given {@code PropertyAccessException} to an appropriate
* error registered on the given {@code Errors} instance.
* <p>Note that two error types are available: {@code FieldError} and
* {@code ObjectError}. Usually, field errors are created, but in certain
* situations one might want to create a global {@code ObjectError} instead.
* @param ex the {@code PropertyAccessException} to translate
* @param bindingResult the errors object to add the error(s) to.
* You can add more than just one error or maybe even ignore it.
* The {@code BindingResult} object features convenience utils such as
* a {@code resolveMessageCodes} method to resolve an error code.
* @see Errors
* @see FieldError
* @see ObjectError
* @see MessageCodesResolver
* @see BeanPropertyBindingResult#addError
* @see BeanPropertyBindingResult#resolveMessageCodes
*/
void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult);
/**
* コンストラクタです。
* @param e 例外
* @param binding バインディング結果
*/
public TypeOrFormatMismatchException(PropertyAccessException e, BindingResult binding) {
super(e.getPropertyChangeEvent(), generateMessage(e, binding), null);
this.e = e;
}