![](/img/trans.png)
[英]Named parameters in spring/hibernate JSR-303 (Bean validation) localized messages
[英]JSR-303 with Spring and Hibernate mixed error messages
我想要做的是有一個標准的錯誤消息,使用注釋填寫上述錯誤消息的值。 代碼應該有助於解釋。
我有一個模型POJO,其中包含JSR-303注釋的字段:
@Min(value = 1)
@Max(value = 9999)
private int someInt;
現在,我有兩個屬性文件:一個用於保存我的字段的標簽,另一個用於保存實際的消息:
label.someInt=Some Integer Field
Min="{0}" must be no less than {value}
然后我循環遍歷構建我想要的東西:
for (FieldError a : result.getFieldErrors()) {
boolean found = false;
for (String b : a.getCodes()) {
System.out.println(b);
try {
addErrorMessage(messageSource.getMessage(b,
new Object[] { new DefaultMessageSourceResolvable(
new String[] { "label." + a.getField() }
)},
null));
found = true;
break;
} catch (Exception e){/* nom Exception */}
}
if (!found) {
addErrorMessage(a.getDefaultMessage());
}
}
這一切都很好,但我希望能夠堅持基於注釋的做事方式並在字段上添加某種標簽。 例如:
@Min(value = 1)
@Max(value = 9999)
@Label(value = "Some Integer Field")
private int someInt;
然后我可以編寫一個輔助函數,它只使用注釋來標記我在屬性文件中的錯誤消息。 我還需要這個來處理基於Spring的驗證錯誤,比如typeMismatch。 有趣的東西,那。
在此先感謝您的幫助!
看看這個論壇: http : //forum.springsource.org/archive/index.php/t-83353.html
或者您可以使用DefaultMessageCodesResolver支持的標准消息密鑰
它看起來已經實現了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.