gpt4 book ai didi

java - 在 Spring MVC 验证中,是否可以一次只显示每个字段的一条错误消息?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:12:30 25 4
gpt4 key购买 nike

例子,

我有

@NotEmpty //tells you 'may not be empty' if the field is empty
@Length(min = 2, max = 35) //tells you 'length must be between 2 and 35' if the field is less than 2 or greater than 35
private String firstName;

然后我输入一个空值。

它说,'不能为空长度必须在 2 到 35' 之间

是否可以告诉 spring 对每个字段一次验证一个?

最佳答案

是的,这是可能的。只需像这样创建您自己的注释:

@Documented
@Constraint(validatedBy = {})
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@ReportAsSingleViolation
@NotEmpty
@Length(min = 2, max = 35)
public @interface MyAnnotation {

public abstract String message() default "{mypropertykey}";

public abstract Class<?>[] groups() default {};

public abstract Class<?>[] payload() default {};
}

重要的部分是@ReportAsSingleViolation 注释

关于java - 在 Spring MVC 验证中,是否可以一次只显示每个字段的一条错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7555201/

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