我的难题与@AssertTrue批注以及Thymeleaf有关。实际上,我应该在注册网页上检查密码是否相等,为此,我在Registrartion Form类中有一个参数,它们是名称,密码,check_password,check_password_condition,地址。实际上,自从在这里提出问题以来,我已经在y代码中进行了一些更改。我使用了equals()
方法而不是==
并按照此帖子Spring validation @AssertTrue已经设置了验证属性boolean check_password_condition
,但是我的代码仍然无法正常工作。这样,我使用Errors
界面向页面询问验证规则。我认为对我的方法使用@AssertTue注释会自动从isCheckpassword()
类调用我的方法RegistrationForm
,然后在Controller的@PostMapping
方法中要求验证规则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; }
@PostMapping("/register") String registerNew(@Valid RegistrationForm form, Errors result) { if (result.hasErrors()) { return "register"; } customerManagement.createCustomer(form); return "redirect:/"; }
但是当不满足创建新用户的条件时出现白页错误。
这里另外,我提供了我的Thymeleaf代码:
<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>
这是我的班级注册
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; } }
请在以下情况下帮助解决此问题来自Whitelabel错误页面的错误信息是:
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)
我的难题与@AssertTrue批注以及Thymeleaf有关。实际上,我应该在注册网页上检查密码是否相等,为此,我在...
它可能无法完全解决您的问题,但看起来String
比较不正确,因为您不应该使用String
。