方法上的Bean验证 [英] Bean Validation on method

查看:144
本文介绍了方法上的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
        }
    }
}

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

My validation will pass if both my passwords are not null, but they are different. Seems like the last constraint will not be taken into account, though I do not have a clue why. Does anyone have suggestion?

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

And no, I am not searching for any implementations of @Matches or simular custom validators. I just would like to get this problem solved.

提前致谢。

更新1

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

I have run some tests regarding this, hopefully the results will provide needed information.

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;

    @PostConstruct
    public void init() {
        context = FacesContext.getCurrentInstance();
        validator = Validation.buildDefaultValidatorFactory().getValidator();

        example1 = "abc";
        example2 = "def";
        runValidation(false, 1);

        example1 = "abc";
        example2 = "abc";
        runValidation(true, 2);

        example1 = "abc";
        example2 = null;
        runValidation(false, 3);
    }

    private void runValidation(boolean assertion, int testNr) {
        FacesMessage message;
        violations = validator.validate(this);
        if(violations.isEmpty() == assertion) {
            message = new FacesMessage("Passed test nr. " + testNr);
        }
        else {
            message = new FacesMessage("Failed test nr. " + testNr);
        }
        context.addMessage(FacesMessage.FACES_MESSAGES, message);
    }

index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        #{bean}
        <h:messages />
    </body>
</html>

结果

beanvalidation.Bean@31c5c3da

    Failed test nr. 1
    Passed test nr. 2
    Passed test nr. 3 


推荐答案

examplesMatch()不是有效的 Java Bean 布尔属性getter。它需要以 get 开头。

examplesMatch() is not a valid Java Beans boolean property getter. It needs to start with get or is.

这篇关于方法上的Bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆