我也不喜欢默认消息,并自定义了我自己的 BindingErrorProcessor。
基本上,我想要的通常只是“最后一个字段”的名称——我想说的是 Date 的值无效,或者 Staff 的值无效,等等。我还包括了被拒绝的字段文本,标准 Spring 错误处理器不提供给消息。
public class SimpleMessage_BindingErrorProcessor
extends DefaultBindingErrorProcessor
{
@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 rejectedValue = ex.getValue();
if (rejectedValue != null && rejectedValue.getClass().isArray()) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
}
Object[] arguments = getArgumentsForBindError( bindingResult.getObjectName(), field, rejectedValue);
FieldError fieldError = new FieldError(
bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage());
bindingResult.addError( fieldError);
}
/**
* Return FieldError arguments for a binding error on the given field.
* <p>TW's implementation returns {0} simple field title, {1} rejected value, {2} FQ field resolvable as per Spring DefaultBindingErrorProcessor
* (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes).
* @param objectName the name of the target object
* @param propPath the field that caused the binding error
* @param rejectedValue the value that was rejected
* @return the Object array that represents the FieldError arguments
* @see org.springframework.validation.FieldError#getArguments
* @see org.springframework.context.support.DefaultMessageSourceResolvable
*/
protected Object[] getArgumentsForBindError (String objectName, String propPath, Object/*String*/ rejectedValue) {
// just the Simple Name of Field;
// (last field in path).
//
String lastField = getLastField_Title( propPath);
// create Resolvable for "Fully-Qualified" Field;
// -- Spring standard, too specific/ would require defining hundreds of distinct messages; we don't use these.
//
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + propPath, propPath};
DefaultMessageSourceResolvable fqField_resolvable = new DefaultMessageSourceResolvable(codes, propPath);
// return Args; {0} simple name, {1} rejected text, {2} FQ complex name.
return new Object[]{
lastField, rejectedValue, fqField_resolvable
};
}
/**
* Return FieldError arguments for a binding error on the given field.
* <p>TW's implementation returns {0} simple field title, {1} FQ field resolvable as per Spring DefaultBindingErrorProcessor
* (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes).
* @param objectName the name of the target object
* @param propPath the field that caused the binding error
* @return the Object array that represents the FieldError arguments
* @see org.springframework.validation.FieldError#getArguments
* @see org.springframework.context.support.DefaultMessageSourceResolvable
*/
@Override
protected Object[] getArgumentsForBindError (String objectName, String propPath) {
// just the Simple Name of Field;
// (last field in path).
//
String lastField = getLastField_Title( propPath);
// create Resolvable for "Fully-Qualified" Field;
// -- Spring standard, too specific/ would require defining hundreds of distinct messages; we don't use these.
//
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + propPath, propPath};
DefaultMessageSourceResolvable fqField_resolvable = new DefaultMessageSourceResolvable(codes, propPath);
// return Args; {0} simple name, {2} FQ complex name.
return new Object[]{
lastField, fqField_resolvable
};
}
protected String getLastField_Title (String propPath) {
int index = propPath.lastIndexOf('.');
String title = (index >= 0) ? propPath.substring(index+1) : propPath;
return StrUtil.capitalize( title);
}
}
这很好用!现在你所有的 messages.properties 必须说的是:
# Type Mismatch generally;
# INCOMING 21/8/13 -- use {0} as 'Simple Name' of field, when using SimpleMessage_BindingErrorProcessor; {1} is 'resolvable' FQN of field.
#
typeMismatch=Invalid value for {0}: "{1}"
# Method Invocation/ value conversion;
# INCOMING 21/8/13 -- only expected for certain 'Value Converting'/ self-parsing properties; SPEC.
#
methodInvocation.machine=Invalid value for {0}: "{1}"
这个区域不是很清楚..整个绑定 -> 错误处理 -> 消息解析系统相当复杂,并且(据我所见)卡住了消息代码通常过于具体的问题。
这方面的内容很少(我在 Google 上没有找到任何直接相关的内容),所以我希望这对人们有所帮助。