spring - 注释 @AssertTrue 无法正常工作
问题描述
我的问题与@AssertTrue 注释有关,以及与Thymeleaf 有关。实际上我应该在注册网页上检查密码是否相等,为此我在我的注册表单类中有一个参数,它们是名称,密码,check_password,check_password_condition,地址。实际上,自从我在这里提出问题以来,我已经对 y 代码进行了一些更改。我使用equals()
了方法而不是根据==
这篇文章
Spring validation @AssertTrue
设置了一个验证属性boolean check_password_condition
,但我的代码仍然无法正常工作。这样,我使用Errors
界面向我的页面询问验证规则。我认为对我的方法使用注释@AssertTue 会自动isCheckpassword()
从RegistrationForm
类中调用我的方法,然后在@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)
解决方案
它可能无法完全解决您的问题,但看起来您的String
比较不正确,因为您不应该使用==
.
相反,使用String#equals()
方法甚至Objects.equals()
. 这个答案提供者对此做了很好的解释。
这是您的代码的样子:
@AssertTrue
public boolean checkPasswod() {
return Objects.equals(check_password, password);
}
推荐阅读
- ssis - 作业失败并出现错误:无法将行添加到输出缓冲区 SSIS
- python - 如何检查一个项目是否在 PHP 的 list.txt 中?
- amazon-s3 - AWS S3 将参数添加到签名的 url
- java - 2个值之间的硒比较失败
- css - React&TypeScript:样式表对象的类型是什么?
- angular - Angular Material Stepper,每个步骤都有单独的组件 - ExpressionChangedAfterItHasBeenCheckedError
- java - Spring MethodArgumentNotValidException 字段始终为空
- python - Impyla 连接。无法启动 SASL。没有可用的机制
- python - 使用绝对路径配置 pytest.ini
- php - 如何从 RabbitMQ que 获取消息并使用 twillio API 将其发送到用户手机