所以我想使用标准格式的错误消息。我不想指定默认代码以外的其他内容。
示例:
domain.property.minSize.notmet=I like purple {?}.
所以我循环了我的错误:
domain.errors.fieldErrors.each {
println g.message(error: it, args: ['bunnies'])
}
这不能正常工作,因为我认为由于默认错误消息已经定义了前x个参数,并且该数字会根据错误的内容而改变。如何在不努力知道'?'
应该是什么数字的情况下指定标准错误代码的参数?
所以我走了这条路,希望没有一些方便的选择。我用我自己的实现覆盖了messageSource bean,只是添加了一个方法:
public class MessageSource extends PluginAwareResourceBundleMessageSource implements GrailsApplicationAware, PluginManagerAware, InitializingBean {
public final String getMessage(MessageSourceResolvable resolvable, Object[] args, Locale locale)
throws NoSuchMessageException {
Object[] arguments;
if (args == null) {
arguments = resolvable.getArguments();
} else {
arguments = args;
}
String[] codes = resolvable.getCodes();
if (codes == null) {
codes = new String[0];
}
for (String code : codes) {
String msg = getMessageInternal(code, arguments, locale);
if (msg != null) {
return msg;
}
}
String defaultMessage = resolvable.getDefaultMessage();
if (defaultMessage != null) {
return renderDefaultMessage(defaultMessage, arguments, locale);
}
if (codes.length > 0) {
String fallback = getDefaultMessage(codes[0]);
if (fallback != null) {
return fallback;
}
}
throw new NoSuchMessageException(codes.length > 0 ? codes[codes.length - 1] : null, locale);
}
}
这几乎是其他方法之一的精确副本,我刚刚修改为接受参数。我遇到的问题是它永远不会找到我的错误消息,总是回到默认状态。 msg
始终为空。
编辑:只是尝试使用标准方法,msg
始终为null也是如此。