根据操作Spring MVC进行验证的最佳实践 [英] Best practices for validation depending on actions Spring MVC

查看:118
本文介绍了根据操作Spring MVC进行验证的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring验证执行验证。我想知道执行验证的最佳做法是什么,主要取决于用户的行为,此后,我有三种不同的方法,但我不知道哪种方法最好。

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.

假设,我们有以下类Foo验证和许多不同的验证规则取决于用户执行的操作。

Assume, we have the following class Foo to validate and many different rules of validation depending on action performed by user.

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)

解决方案1:每个验证规则一个验证器类

solution 1: one validator class per validation rule

public class CreateFooValidator implements Validator {
    //validation for action create
}
public class EvaluateFooValidator implements Validator {
    //validation for evaluate action
}

解决方案2:一个带有多种方法的验证器类

solution 2: one validator class with several methods

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
}

解决方案3:Foo类中的附加属性操作,一个验证器

solution 3: Additional property action in class Foo, one validator

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
}

3或其他解决方案中最好的解决方案是什么?
谢谢!

What is the best solution among the 3 or other solution if any? Thanks!

推荐答案

使用JSR-303验证组,自从Spring MVC 3.1也支持 @Validated

Use JSR-303 Validation Groups, which are since Spring MVC 3.1 also supported by @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 {
}

使用包括该组的验证注释注释 Foo ,例如

Annotate Foo with the validation annotations including the group, e.g.

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;

    ...
}

然后注释 foo 控制器方法的参数以及相应的 @Validated 注释,例如对于 evaluate 控制器方法, @Validated({Evaluate.class})

Then annotate the foo argument of your controller methods with the appropriate @Validated annotation, e.g. @Validated({Evaluate.class}) for the evaluate controller method.

你可以在这里找到另一个例子(见第2项):
http://blog.goyello.com/2011/12/16/enhancements-spring-mvc31/

You can find another example here (see item 2): http://blog.goyello.com/2011/12/16/enhancements-spring-mvc31/

更新:
或者,如果由于某种原因你不能/不想使用 @Validated ,你可以使用注入的 Validator 实例并将组传递给其 validate 方法。这就是在Spring 3.1之前完成的方式(正如评论文章中的情况一样)。

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).

这篇关于根据操作Spring MVC进行验证的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆