胸腺形式验证
来源:爱站网时间:2021-09-16编辑:网友分享
我正在尝试通过仔细检查来验证密码,如果密码不匹配,则显示百里香素错误。输入形式如下:com.foxminded.university.domain导入javax ....
问题描述
我正在尝试通过仔细检查来验证密码,如果密码不匹配,则会显示百里香素错误
输入表单看起来像这样
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);
}
}
和百里香叶形式行:
但是,使用此设置,它不会加载异常Caused by: java.lang.NullPointerException at com.foxminded.university.domain.UserForm.isPasswordsEqual(UserForm.java:74)
所以我不确定应该在百里香模板中使用什么字段进行注释,并在百里香模板中使用它?
思路:
也许在任何运行时密码为空,尝试添加验证以避免空指针。
@AssertTrue(message = "PasswisPasswordsEqualords should match")
public boolean isPasswordsEqual() {
return (password == null) ? false : password.equals(passwordRepeat);
}