我有以下類,我試圖通過Hibernate來驗證:如何全局設置Hibernate驗證約束的消息?
public class LoginForm
{
@NotBlank(message = "{myCustomMessage}")
private String user;
@NotBlank(message = "{myCustomMessage}")
private String pw;
//....
}
這工作正常,並正確顯示NotBlank
我的自定義消息,但是如果我必須指定(message = "{myCustomMessage}")
線爲每一個約束,對於每一個單獨的成員,每一個班級都會變得非常重複。
是否有一種方法可以全局設置此消息,因此不必爲每個成員單獨指定? I.e因此,默認情況下,將獲取myCustomMessage
爲@NotBlank
? (以及我指定的所有其他約束條件)。
編輯:所以如果我將消息密鑰從myCustomMessage
重命名爲org.hibernate.validator.constraints.NotBlank.message
,那麼我不再需要指定(message..)
行。不過,我仍然希望能夠在全球範圍內使用myCustomMessage
。
請看看這個以前的答案:http://stackoverflow.com/questions/2235544/bean-validation-and-error-messages-at-properties-file – Romski
@Romski這不回答我的問題 –
對不起,只是重新閱讀你的問題,我看到你的問題。除了編輯中的解決方案之外,我不知道其他任何內容。 – Romski