相关疑难解决方法(0)

方法上的Bean验证

public class Register {
    @NotNull private String password;
    @NotNull private String passwordRepeat;
    @AssertTrue private boolean comparePasswords() { 
       return password.equals(passwordRepeat);
    }

    private Set<ConstraintViolation<Register>> violations;

    public void doRegister(AjaxBehaviorEvent event) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        violations = validator.validate(this);

        if(violations.isEmpty()) {
            // This occurs
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我的密码都不为空,我的验证将通过,但它们是不同的.似乎最后的约束似乎不会被考虑在内,尽管我不知道为什么.有没有人有建议?

不,我不是在寻找@Matches或simular自定义验证器的任何实现.我只想解决这个问题.

提前致谢.

更新1

我已经对此进行了一些测试,希望结果将提供所需的信息.

Bean.java

@Named
@RequestScoped
public class Bean {
    @NotNull private String example1;
    @NotNull private String example2;
    @AssertTrue private boolean examplesMatch() { return example1.equals(example2); }

    private Set<ConstraintViolation<Bean>> violations;
    private FacesContext context;
    private Validator validator; …
Run Code Online (Sandbox Code Playgroud)

java jsf bean-validation

2
推荐指数
1
解决办法
2818
查看次数

标签 统计

bean-validation ×1

java ×1

jsf ×1