0

My poblem is connected with @AssertTrue annotation and some how with Thymeleaf. Actualy I should scheck password for the equality on a registration web page, for this I hava a parameters in my Registrartion Form class, they are Name,password, check_password,check_password_condition, adress. Actually I have made several changes in y code since I have aske my question here. I have used equals() method instead of == and acording this post Spring validation @AssertTrue have set a validation property boolean check_password_condition but my code still does not work. This way I use Errors interface to ask my page for validation rules. I think that using of the annotation @AssertTue for my method sould automatically call my method isCheckpassword() from RegistrationForm class and then in @PostMapping method of the Controller asked for the validation rule this.password.equals(this.check_password) Am I right or not????

@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;
    }
    @PostMapping("/register")
    String registerNew(@Valid RegistrationForm form, Errors result) {

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

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

But I get whitepage error when the conditions for the creating new user are not met.

here additionally I provide my Thymeleaf code:

        <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>

This is my Registration From class

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;
    }
}

Please help to solve this problem when error info from Whitelabel errorpage is:

    Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('*')" (template: "register" - line 29, col 42)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:125)
Fedo
  • 349
  • 1
  • 7
  • 1. Don't compare strings with `==` us `equals. 2. The `@AssertTrue` needs to be on a non-args method. You want to compare the fields int he entity. – M. Deinum Nov 05 '19 at 14:17
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper Nov 05 '19 at 15:04
  • @M. Deinum I have not understand, how then I can compare two variables with AssertTrue annotation if the method that compare then cannot get any variables? – Fedo Nov 07 '19 at 11:42
  • The method is part of the object containing the attributes. Your method implementation should read something like `return this.password != null ? Objects.equals(this.password, this.check_password) : false`. – M. Deinum Nov 07 '19 at 11:42
  • @M. Deinum I have updated my question please could you see it again? Thanks in advance – Fedo Nov 09 '19 at 20:19
  • What is there to check? Why on earth are you setting a property and return that property. – M. Deinum Nov 11 '19 at 06:44

1 Answers1

0

It may not fully solve your issue, but looks like your String comparison is incorrect, as you shouldn't use ==.

Instead, use the String#equals() method or even Objects.equals(). This answer provider a great explanation on this.

Here's what your code can be like:

@AssertTrue
public boolean checkPasswod() {
    return Objects.equals(check_password, password);
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thanks! I have solved this problem, but still I have a question how can I make my @AssertTrue method non-args – Fedo Nov 07 '19 at 11:43