Java 根据 Spring MVC 的操作进行验证的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19089649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practices for validation depending on actions Spring MVC
提问by aslan
I am trying to perform validation using Spring validation. I am wondering what is the best practice to perform validation that depends mainly on user's action, hereafter, I have three different approaches but I don't know which one is best.
我正在尝试使用 Spring 验证执行验证。我想知道执行主要取决于用户操作的验证的最佳实践是什么,此后,我有三种不同的方法,但我不知道哪种方法最好。
Assume, we have the following class Foo to validate and many different rules of validation depending on action performed by user.
假设,我们有以下 Foo 类要验证,以及许多不同的验证规则,具体取决于用户执行的操作。
public class Foo {
private String name;
private int age;
private String description;
private int evaluation;
// getters, setters
}
What is the best way to perform these validations (for instance: during creation only name and age are required, during evaluate action, I only need evaluation to be validated and so on)
执行这些验证的最佳方法是什么(例如:在创建期间只需要姓名和年龄,在评估操作期间,我只需要验证评估等)
solution 1: one validator class per validation rule
解决方案 1:每个验证规则一个验证器类
public class CreateFooValidator implements Validator {
//validation for action create
}
public class EvaluateFooValidator implements Validator {
//validation for evaluate action
}
solution 2: one validator class with several methods
解决方案 2:一个具有多种方法的验证器类
public class FooValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Foo.class.equals(clazz);
}
//the default validate method will be used to validate during create action
@Override
public void validate(Object target, Errors errors) {
//validation required
}
//method to validate evaluate action
public void validateFooEvaluation(Object target, Errors errors) {
//validation required
}
//other validation methods for other actions
}
solution 3: Additional property action in class Foo, one validator
解决方案 3:Foo 类中的附加属性操作,一个验证器
public class Foo {
private String name;
private int age;
private String description;
private int evaluation;
private String actionOnFoo;
// getters, setters
}
public class FooValidator implements Validator {
private final Foo foo = (Foo) target;
@Override
public boolean supports(Class<?> clazz) {
return Foo.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
//test which action is performed by user
if ("create".equals(foo.getActionOnFoo())) {
//call for private method that performs validation for create action
}
}
//all private methods
}
What is the best solution among the 3 or other solution if any? Thanks!
3个或其他解决方案中的最佳解决方案是什么?谢谢!
采纳答案by herman
Use JSR-303 Validation Groups, which are since Spring MVC 3.1 also supported by @Validated
.
使用 JSR-303 验证组,自 Spring MVC 3.1 以来,@Validated
.
So, for each action you should have a method in your controller. Create a validation group for each possible action that has a different set of rules, e.g.
因此,对于每个操作,您的控制器中都应该有一个方法。为每个具有不同规则集的可能操作创建一个验证组,例如
public interface Create {
}
public interface Evaluate {
}
Annotate Foo
with the validation annotations including the group, e.g.
标注Foo
与确认的注释包括基团,例如
public class Foo {
@NotNull(groups = { Create.class, Evaluate.class })
private String name;
...
@Min(value = 1, message = "Evaluation value needs to be at least 1", groups = { Evaluate.class })
private int evaluation;
...
}
Then annotate the foo
argument of your controller methods with the appropriate @Validated
annotation, e.g. @Validated({Evaluate.class})
for the evaluate
controller method.
然后注释foo
与适当的控制器方法参数@Validated
的注释,例如@Validated({Evaluate.class})
对evaluate
控制器的方法。
You can find another example here (see item 2): http://blog.goyello.com/2011/12/16/enhancements-spring-mvc31/
您可以在此处找到另一个示例(请参阅第 2 项):http: //blog.goyello.com/2011/12/16/enhancements-spring-mvc31/
Update:
Alternatively, if for some reason you can't / don't want to use @Validated
, you can use an injected Validator
instance and pass the groups to its validate
method. That's the way it was done before Spring 3.1 (as is the case in the article in your comment).
更新:或者,如果由于某种原因你不能/不想使用@Validated
,你可以使用注入的Validator
实例并将组传递给它的validate
方法。这是在 Spring 3.1 之前完成的方式(就像您评论中的文章中的情况一样)。
回答by Gerard Ribas
Depends that you want to do it. All solutions are good if you apply in all of your validators the same and you want to validate some business logic, not only if it's null or not (for this purpose you can use the @Valid annotation and annotation @Not null in your objects).
取决于你想这样做。如果您在所有验证器中应用相同并且想要验证某些业务逻辑,而不仅仅是它是否为 null(为此,您可以在对象中使用 @Valid 注释和注释 @Not null),那么所有解决方案都很好.
For me, if I have an object Foo I only want one Validator for this object, and then I have several methods for validate the data depends of I need in my controllers, for example, one for saving new Foo or other one for validating before updating, etc...
对我来说,如果我有一个对象 Foo,我只想要一个用于该对象的验证器,然后我有几种方法来验证数据,这取决于我在控制器中需要的数据,例如,一种用于保存新的 Foo 或另一种用于之前验证更新等...