我正在学习 Spring Framework 和 Thymeleaf。我已经知道如何使用类似${#fields.errors("xx")}. 但是,我对如何在 Thymeleaf 中显示对象错误消息感到困惑。
这是我的UserForm类:
@PasswordMatches
public class UserForm {
@NotNull
@NotEmpty
private String username;
@NotNull
@NotEmpty
private String password;
@NotNull
@NotEmpty
private String matchingPassword;
@NotNull
@NotEmpty
@ValidEmail
private String email;
/* setter and getter methods */
Run Code Online (Sandbox Code Playgroud)
这是我的PasswordMatches注释:
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PasswordMatchesValidator.class)
@Documented
public @interface PasswordMatches {
String message() default "Passwords don't match";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
class PasswordMatchesValidator implements …Run Code Online (Sandbox Code Playgroud)