我正在运行此代码。如果我向1个字段添加验证注释,则会正确选取违规。但是,如果我在多个字段上添加注释,则根本不会发现任何违规行为。
public class Address implements Serializable {
private static final long serialVersionUID = 225035939665473333L;
@NotNull
@NotEmpty
@Size(min = 4, max = 50, message="test")
private String Name;
@NotNull
@NotEmpty
private String AddressLine1;
private String AddressLine2;
private String PostalCode;
private String Province;
@NotNull
@NotEmpty
private String Country;
//getter and setters below this line
}
我正在运行此代码:
ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();
Address a = new Address();
a.setName("a");
Set<ConstraintViolation<Address>> violations = validator.validate(a);
Window.alert(String.valueOf(violations.size()));
for(ConstraintViolation<Address> violation : violations){
Window.alert(violation.getMessage());
}
这将获得零错误,即使它应该拾取3个错误。 AddressLine1,Country不应为null。名字太短。如果我将类更改为下面的代码,它将会发现名称太短并显示错误消息&#34; test&#34;。
public class Address implements Serializable {
private static final long serialVersionUID = 225035939665473333L;
@NotNull
@NotEmpty
@Size(min = 4, max = 50, message="test")
private String Name;
private String AddressLine1;
private String AddressLine2;
private String PostalCode;
private String Province;
private String Country;
//getter and setters below this line
}