小编DCe*_*Vie的帖子

Spring 类级别验证和 Thymeleaf

我正在学习 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)

validation spring thymeleaf

5
推荐指数
2
解决办法
1413
查看次数

标签 统计

spring ×1

thymeleaf ×1

validation ×1