18
A
回答
40
JSR 303 default message interpolation algorithm允許您通過提供名爲ValidationMessages的資源包來自定義消息。在包含classpath中創建一個ValidationMessages.properties
文件:
javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE
javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE
這改變了默認消息爲@Size
約束,所以你應該使用@Size
約束,而不是Hibernate的特定@Length
約束。
除了更改所有約束的默認消息,您可以更改特定約束實例的消息。設置message
屬性上的約束:
@NotNull(message = "{email.notnull}")
private String email;
和消息添加到ValidationMessages.properties
文件:
email.notnull=E-mail address is required
0
By Spring我假設你的意思是Spring MVC。
從下面的參考 http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
在這裏你去 -
您創建一個驗證器類 -
public class UserValidator implements Validator {
public boolean supports(Class candidate) {
return User.class.isAssignableFrom(candidate);
}
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
}
}
在你在上面領域所需的任何驗證文本把。
在JSP中,你將需要以下標籤 -
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
<!-- Show errors for firstName field -->
<td><form:errors path="firstName" /></td>
</tr>
這樣的firstName
任何驗證錯誤將被打印出來。
相關問題
- 1. Spring驗證錯誤生成
- 2. Spring驗證器Junit錯誤
- 3. Spring + Hibernate驗證錯誤
- 4. Spring使用錯誤的編碼來定製驗證消息
- 5. 錯誤定製驗證C#MVC
- 6. Laravel 4定製驗證錯誤消息
- 7. 用貓鼬定製驗證錯誤
- 8. 我如何定製rails3驗證錯誤?
- 9. Drupal:定製驗證錯誤信息
- 10. Spring MVC驗證錯誤樣式
- 11. Spring Security UI驗證註冊錯誤
- 12. Spring MVC i18n驗證錯誤消息
- 13. Spring數據MongoDB身份驗證錯誤
- 14. Spring Security:驗證用戶的403錯誤
- 15. Spring MVC 3和驗證錯誤
- 16. spring 4.3中的applicationContext.xml驗證錯誤?
- 17. spring-data-elasticsearch中的XSD驗證錯誤
- 18. Spring驗證錯誤的文檔
- 19. Spring MVC JSR 303從ValidationMessages.properties驗證Json響應驗證錯誤
- 20. facebook錯誤'驗證驗證碼錯誤'
- 21. 驗證錯誤與驗證錯誤
- 22. Spring MVC驗證:爲每個驗證檢查設置特定的錯誤消息
- 23. Spring驗證@AssertTrue自定義錯誤代碼/消息
- 24. Spring Security的401錯誤自定義驗證過濾器
- 25. 如何自定義Spring @Valid驗證的默認錯誤消息?
- 26. 如何確定證書驗證錯誤
- 27. 驗證錯誤
- 28. 驗證錯誤
- 29. 驗證錯誤
- 30. 驗證錯誤
我用註釋驗證像@NotNull。那麼在哪裏存儲消息在Spring jar?我認爲我需要編輯他們 – 2011-01-26 14:27:19
@ apache-fan我投下你的答案,因爲OP說關於現代JSR303風格的驗證,而不是關於舊的春天特定。 – 2011-10-05 17:41:45