本文介绍了在Spring MVC验证中,是否可以一次只显示每个字段一条错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例,
我有
@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之间'
It says, 'may not be emptylength must be between 2 and 35'
是否有可能告诉spring每个字段一次验证一个?
Is it possible to tell spring to validate one at a time per field?
推荐答案
是的,这是可能的。只需像这样创建自己的注释:
Yes it is possible. Just create your own annotation like this:
@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注释
important part is the @ReportAsSingleViolation annotation
这篇关于在Spring MVC验证中,是否可以一次只显示每个字段一条错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!