Validation GWT JSR303验证、验证方法或使用自定义注释

Validation GWT JSR303验证、验证方法或使用自定义注释,validation,gwt,hibernate-validator,Validation,Gwt,Hibernate Validator,我正在尝试使用GWT的2.5内部版本验证特性。我有一些复杂的验证。 建议我要么包含进行验证的方法,要么编写自己的注释。然而,两者都不起作用 public class PageData extends Serializable @NotNull(message="Cannot be null!") Boolean value @AssertTrue(message="isValid() is false!") private boolean isValid() {

我正在尝试使用GWT的2.5内部版本验证特性。我有一些复杂的验证。 建议我要么包含进行验证的方法,要么编写自己的注释。然而,两者都不起作用

public class PageData extends Serializable
    @NotNull(message="Cannot be null!")
    Boolean value

    @AssertTrue(message="isValid() is false!")
    private boolean isValid() {
        return false;
    }

    //Getters and Setters
}
验证布尔值。但是,永远不会调用/验证isValid()。但是为什么呢? 这是GWt特有的问题吗


然后,我尝试编写自己的注释,@FieldMatch中的示例使用ApacheCommonsBeanutils中的bean.getProperty(),这在GWT中是无法使用的。有什么方法可以让这些复杂的注释在GWT中工作吗?

下面是我如何创建一个自定义验证的方法,它可以跨一个bean的多个字段工作。它检查当联系人bean的ContactProfile字段设置为COMPANY时,必须填写COMPANY name,否则设置为PERSON时,必须填写first name或last name:

注释定义:

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ValidCompanyOrPersonValidator.class)
public @interface ValidCompanyOrPerson {

    String message() default "{contact.validcompanyorperson}";

    Class<?>[] groups() default {};

    Class<? extends Contact>[] payload() default {};
}
public class ValidCompanyOrPersonValidator implements ConstraintValidator<ValidCompanyOrPerson, Contact> {

    ValidCompanyOrPerson annotation;

    public void initialize(ValidCompanyOrPerson annotation) {
        this.annotation = annotation;
    }

    @SuppressWarnings("nls")
    public boolean isValid(Contact contact, ConstraintValidatorContext context) {
        boolean ret = false;
        if (contact.getContactProfile() == null) {
        } else if (contact.getContactProfile().equals(ContactProfile.COMPANY)) {
            ret = (contact.getCompanyName() != null);
        } else if (contact.getContactProfile().equals(ContactProfile.PERSON)) {
            ret = (contact.getGivenName() != null || contact.getFamilyName() != null);
        }
        return ret;
    }
}
我可以在客户端(GWT)和服务器端使用此验证


希望这会有帮助……

@chriscross有帮助吗?@xybrek是的,有帮助。什么是有效的公司或个人注释;对于什么时候不用?只是curious@xybrek我的联系人类作为枚举字段类型,可以设置为个人或公司;验证程序确保标记为person时,字段givenName或familyName不为null,标记为company时,companyName不为null。
@ValidCompanyOrPerson
public class Contact  {
 ...
}