使用 Spring 3 验证向导页面

发布于 2024-11-24 08:31:06 字数 440 浏览 1 评论 0原文

我开始研究如何在 Spring 中为类似向导的表单创建控制器,并遇到了 AbstractWizardFormController,我很快注意到它已被弃用。

然后我进一步挖掘,发现如何使用 Spring 3 完成类似的事情。不过,这个示例没有进行任何类型的验证(例如通过@Valid),所以我想知道如何验证向导的每个步骤?

每个步骤是否可以有自己的支持 Form 对象,然后使用 @SessionAttributes 来存储调用最终提交时的值(大概在表单的最后一页上)?

感谢您的任何帮助。

(PS:不需要 WebFlow 的解决方案是理想的。)

I started researching how to create a controller for a wizard-like form in Spring and came across the AbstractWizardFormController, which I quickly noticed was deprecated.

I then dug a bit further and found how to accomplish something similar with Spring 3. This example does not do any sort of validation though (e.g. via @Valid), so I'm wondering how does one validate each step of a wizard?

Would it be possible for each step have its own backing Form object and then use @SessionAttributes to store their values for when a final submit is called (presumably on the last page of the form)?

Thanks for any help.

(P.S.: Solutions that don't require WebFlow would be ideal.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2024-12-01 08:31:06

我不知道如何使用 @Valid 注释来实现此目的,但您应该能够利用 JSR-303 验证来完成此操作。作为一个有点人为的示例:

public class User {
    @NotNull(message = "First name can't be blank", groups = {Step1.class, FinalStep.class})
    private String firstName;

    @NotNull(message = "Last name can't be blank", groups = {Step1.class, FinalStep.class})
    private String lastName;

    @NotNull(message = "Email can't be blank", groups = {Step1.class, FinalStep.class})
    private String emailAddress;

    @NotNull(message = "Please provide a valid address", groups = {Step2.class, FinalStep.class})
    private Address address;

    // getters/setters...

    public interface Step1 {}
    public interface Step2 {}
    public interface FinalStep {}
}

您可以通过提供标记接口来表示向导步骤,从而利用 JSR-303 支持验证组的事实。

然后,不要依赖 @Valid 注释,而是将 Validator 实例注入控制器并调用:

validator.validate(user, /*<step interface>.class*/);

在 processPage 方法中(在链接的问题中引用 Controller),然后

validator.validate(user, FinalStep.class);

在 processFinish 调用中调用。

I don't know of a way to pull this off with the @Valid annotation, but you should be able to take advantage of the JSR-303 validation to accomplish this. As a somewhat contrived example:

public class User {
    @NotNull(message = "First name can't be blank", groups = {Step1.class, FinalStep.class})
    private String firstName;

    @NotNull(message = "Last name can't be blank", groups = {Step1.class, FinalStep.class})
    private String lastName;

    @NotNull(message = "Email can't be blank", groups = {Step1.class, FinalStep.class})
    private String emailAddress;

    @NotNull(message = "Please provide a valid address", groups = {Step2.class, FinalStep.class})
    private Address address;

    // getters/setters...

    public interface Step1 {}
    public interface Step2 {}
    public interface FinalStep {}
}

You can take advantage of the fact that JSR-303 supports validation groups by providing marker interfaces to represent your wizard steps.

Then, instead of relying on the @Valid annotation, inject a Validator instance into your controller and call:

validator.validate(user, /*<step interface>.class*/);

in your processPage method (referencing Controller in your linked question), and then

validator.validate(user, FinalStep.class);

in your processFinish call.

风吹雪碎 2024-12-01 08:31:06

使用@Validated

来自 Spring 的文档:

JSR-303 Valid 的变体,支持验证组的规范。设计用于方便使用 Spring 的 JSR-303 支持,但不是 JSR-303 特定的。
例如可以与 Spring MVC 处理程序方法参数一起使用。通过 SmartValidator 的验证提示概念提供支持,验证组类充当提示对象。
也可以与方法级别验证一起使用,指示特定的类应该在方法级别进行验证(充当相应验证拦截器的切入点),但也可以选择在带注释的方法级别验证中指定验证组班级。在方法级别应用此注释可以覆盖特定方法的验证组,但不能用作切入点;然而,类级注释对于触发特定 bean 的方法验证仍然是必要的。还可以用作自定义构造型注释或自定义特定组验证注释的元注释。

Use @Validated.

From Spring's documentation:

Variant of JSR-303's Valid, supporting the specification of validation groups. Designed for convenient use with Spring's JSR-303 support but not JSR-303 specific.
Can be used e.g. with Spring MVC handler methods arguments. Supported through SmartValidator's validation hint concept, with validation group classes acting as hint objects.
Can also be used with method level validation, indicating that a specific class is supposed to be validated at the method level (acting as a pointcut for the corresponding validation interceptor), but also optionally specifying the validation groups for method-level validation in the annotated class. Applying this annotation at the method level allows for overriding the validation groups for a specific method but does not serve as a pointcut; a class-level annotation is nevertheless necessary to trigger method validation for a specific bean to begin with. Can also be used as a meta-annotation on a custom stereotype annotation or a custom group-specific validated annotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文