使用 Spring 3 验证向导页面
我开始研究如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道如何使用 @Valid 注释来实现此目的,但您应该能够利用 JSR-303 验证来完成此操作。作为一个有点人为的示例:
您可以通过提供标记接口来表示向导步骤,从而利用 JSR-303 支持验证组的事实。
然后,不要依赖 @Valid 注释,而是将 Validator 实例注入控制器并调用:
在 processPage 方法中(在链接的问题中引用 Controller),然后
在 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:
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:
in your processPage method (referencing Controller in your linked question), and then
in your processFinish call.
使用@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.