我使用javax.validation
進行JSR-303
bean驗證。javax.validation錯誤ORDER作爲驗證方法的結果條目
在調用validate方法後,我會遍歷結果並在GUI中執行某些操作(例如突出顯示錶單中失敗的字段)。
我的問題在這裏:
MyBean.java
public class MyBean {
@Size(min = 1, message = "Please insert title")
private String title;
@Size(min = 1, message = "Please insert author")
private String author;
@Size(min = 1, message = "Please insert publisher")
private String publisher;
// ... something
}
我的驗證方法:
Set<ConstraintViolation<MyBean>> failures = this.validator.validate(bean);
在那之後,我遍歷我的失敗對象:
for (ConstraintViolation<MyBean> constraintViolation : failures) {
propertyPath = constraintViolation.getPropertyPath().toString();
if (propertyPath.equals("title")) {
Color bg = new Color(242, 242, 251);
this.txtTitle.setBackground(bg);
}
// some else if for other attributes (for example: author)
// add error to all error messages
errorMessage += constraintViolation.getMessage();
}
現在我的問題是:命令這些對象在故障中設置。這是不正確的。 有沒有解決方案來獲得正確的訂單?
感謝所有
我正在做你寫的東西,但問題仍然是:errormessage得到無序,而那不是專業。 (同時我也希望將焦點放在表單的第一個文本框上,並且出現錯誤。) – eav
所以我認爲它取決於您的UI技術,它本身應該依賴於您選擇的Beans Validation實現。您可以搜索JSR 303與您的UI框架的集成,或者告訴我們更多關於您的上下文和您的需求 –