gpt4 book ai didi

java - Thymeleaf 表单验证

转载 作者:行者123 更新时间:2023-12-01 18:14:45 24 4
gpt4 key购买 nike

我正在尝试通过双重检查来验证密码,如果密码不匹配,则使用 thymeleaf 显示错误

输入表单如下所示

package com.foxminded.university.domain;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class UserForm {

@NotNull(message = "Can't be empty")
@Size(min = 2, max = 30, message = "Must be more than 2 and less than 30 symbols")
private String firstName;

@NotNull(message = "Can't be empty")
@Size(min = 2, max = 30, message = "Must be more than 2 and less than 30 symbols")
private String lastName;

@Email(message = "Enter valid e-mail" )
private String email;

@Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})",
message = "Enter valid password")
private String password;

private String passwordRepeat;

private boolean passwordsEqual;

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getPassword() {
return password;
}

public String getPasswordRepeat() {
return passwordRepeat;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setEmail(String email) {
this.email = email;
}

public void setPassword(String password) {
this.password = password;
}

public void setPasswordRepeat(String passwordRepeat) {
this.passwordRepeat = passwordRepeat;
}

public void setPasswordsEqual(boolean passwordsEqual) {
this.passwordsEqual = passwordsEqual;
}
@AssertTrue(message = "Passwords should match")
public boolean isPasswordsEqual() {
return password.equals(passwordRepeat);
}
}

thymeleaf 表格行:

<div class="form-group">
<label>Password</label>
<input type="text" th:unless="${#fields.hasErrors('password')}" class="form-control" placeholder="Password"
th:field="*{password}">
<input type="text" th:if="${#fields.hasErrors('password')}" class="form-control alert-danger"
placeholder="Password" th:field="*{password}">
<span th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>
</div>
<div class="form-group">
<label> Repeat password</label>
<input type="text" th:unless="${#fields.hasErrors('passwordsEqual')}" class="form-control"
placeholder="Password" th:field="*{passwordRepeat}">
<input type="text" th:if="${#fields.hasErrors('passwordsEqual')}" class="form-control alert-danger"
placeholder="Password" th:field="*{passwordRepeat}">
<span class="error" th:if="${#fields.hasErrors('passwordsEqual')}" th:errors="*{passwordsEqual}"></span>
</div><div class="form-group">
<label> Repeat password</label>
<input type="text" th:unless="${#fields.hasErrors('passwordsEqual')}" class="form-control"
placeholder="Password" th:field="*{passwordRepeat}">
<input type="text" th:if="${#fields.hasErrors('passwordsEqual')}" class="form-control alert-danger"
placeholder="Password" th:field="*{passwordRepeat}">
<span class="error" th:if="${#fields.hasErrors('passwordsEqual')}" th:errors="*{passwordsEqual}"></span>
</div>

但是通过此设置,它不会加载异常引起:java.lang.NullPointerException at com.fox Mind.university.domain.UserForm.isPasswordsEqual(UserForm.java:74)

所以我不确定我应该在 thymeleaf 模板中注释什么以及在哪个字段中使用它才能工作?

最佳答案

也许密码在任何运行时都为空,尝试添加验证以避免空指针。

 @AssertTrue(message = "PasswisPasswordsEqualords should match")
public boolean isPasswordsEqual() {
return (password == null) ? false : password.equals(passwordRepeat);
}

关于java - Thymeleaf 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com