我使用 Spring 的 @Valid
注释来验证带有 javax.constraints
注释的 bean 字段.
I use Spring's @Valid
annotation for validate bean's fields which annotated with javax.constraints
annotations.
但是当我需要从验证中排除某些字段时(仅适用于某些情况),我遇到了一个问题.
But I faced a problem when I need to exclude some fields from validation (only for some cases).
我进行了调查,没有发现任何有用的方法,并且大多数答案的日期为 2010-2011.这是相当令人惊讶的,因为这种情况如此普遍.
I did an investigation didn't found any usefull ways and most of answers were dated as 2010-2011. It is quite surprisingly since this situatuion is so common.
Spring 4.+ 从那时起有什么变化吗?或者也许任何人都可以分享如何打败这个的个人经验?
Is there any changes from that times for Spring 4.+? Or maybe anyone can share personal experience how to beat this?
谢谢.
可以使用validation group,@Validated
注释.
You can use validation group, and @Validated
annotation.
http://www 中有详细说明.javacodegeeks.com/2014/08/validation-groups-in-spring-mvc.html
该方法基于 休眠验证器组,它使您能够根据标记接口对验证进行分组并将其应用于处理程序级别,如链接中给出的示例
The approach is based on hibernate validator's groups and it enables you to group the validation based on the marker interface and apply it on handler level, example as given in the link
@RequestMapping(value = "stepTwo", method = RequestMethod.POST)
public String stepTwo(@Validated(Account.ValidationStepTwo.class) Account account, Errors errors) {
if (errors.hasErrors()) {
return VIEW_STEP_TWO;
}
return "redirect:summary";
}
这篇关于从@Valid 验证中排除一些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!