注释@AssertTrue无法正常工作

我的问题与@AssertTrue注释以及Thymeleaf的某些方法有关。实际上,我应该在注册网页上检查密码是否相等,为此,我在Registrartion Form类中有一个参数,它们是名称,密码,check_password,check_password_condition,地址。实际上,自从在这里提出问题以来,我已经在y代码中进行了一些更改。我使用了equals()方法而不是==并按照这篇文章 Spring validation @AssertTrue 已设置验证属性boolean check_password_condition,但我的代码仍然无法正常工作。这样,我使用Errors界面向我的页面询问验证规则。我认为对方法使用注解@AssertTue会自动从isCheckpassword()类调用方法RegistrationForm,然后在Controller的@PostMapping方法中要求验证规则{{1} } 我对吗??

this.password.equals(this.check_password)
@AssertTrue(message = "{RegistrationForm.check_password.AssertTrue}")
    public boolean isCheckpassword(){
        if(this.password.equals(this.check_password)){
            return this.check_password_condition=true;
        }
        else return this.check_password_condition=false;
    }

但是当不满足创建新用户的条件时,我会出现白页错误。

在这里我还提供了我的Thymeleaf代码:

    @PostMapping("/register")
    String registerNew(@Valid RegistrationForm form,Errors result) {

        if (result.hasErrors()) {
            return "register";
        }

        customerManagement.createCustomer(form);
        return "redirect:/";
    }

这是我的班级注册

        <div class="field">
            <label th:text="#{register.check_password}" for="check_password">Repasswort</label>
            <input id="check_password" name="check_password" th:field="*{check_password}" th:errorclass="fieldError" type="password"
                   required="required"/><br/>
            <p th:if="${#fields.hasErrors('check_password')}" th:errors="*{check_password}">Das Passwort darf
                nicht leer sein.</p>
        </div>

请在以下情况下帮助解决此问题 来自Whitelabel错误页面的错误信息是:

class RegistrationForm {
    @NotEmpty(message = "{RegistrationForm.name.NotEmpty}") //
    private final String name;
    @Size(min = 2,max = 14,message = "{RegistrationForm.password.Size}")
    @NotEmpty(message = "{RegistrationForm.password.NotEmpty}") //
    private final String password;
    @NotEmpty(message = "{RegistrationForm.check_password.NotEmpty}") //
    private  String check_password;
    private boolean check_password_condition;
    @NotEmpty(message = "{RegistrationForm.address.NotEmpty}") // s
    private final String address;
    public RegistrationForm(String name,String password,String check_password,String address) {

        this.name = name;
        this.password = password;
        this.check_password=check_password;
        this.address = address;
    }

    @AssertTrue(message = "{RegistrationForm.check_password.AssertTrue}")
    public boolean isCheckpassword(){
        if(this.password.equals(this.check_password)){
            return this.check_password_condition=true;
        }
        else return this.check_password_condition=false;
    }
        //return this.password != null && this.password.equals(this.check_password) : this.setCheck_password(); }

    public String getName() {
        return name;
    }

    public String getPassword() { return password; }

    public String getcheck_password(){return check_password;}


    public String getaddress() {
        return address;
    }
}
daiwang2 回答:注释@AssertTrue无法正常工作

它可能无法完全解决您的问题,但是您的String比较似乎不正确,因为您不应该使用==

相反,请使用String#equals()方法,甚至使用Objects.equals()。该answer提供者对此做了很好的解释。

您的代码如下所示:

@AssertTrue
public boolean checkPasswod() {
    return Objects.equals(check_password,password);
}
本文链接:https://www.f2er.com/3159018.html

大家都在问