验证 JSF 中的类级别 bean 验证约束 [英] Validate class level bean validation constraints in JSF

查看:35
本文介绍了验证 JSF 中的类级别 bean 验证约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSF 2.0 似乎没有调用类级别约束".引用 SO 答案

It seems that JSF 2.0 does not call "class level constraints". Quoting from an SO answer

JSF 2.0 不调用类级别验证约束.来自 JSF 验证: JSF 2提供与 JSR-303 约束的内置集成.当您在应用程序中使用 bean 验证时,JSF 会自动使用 UIInput 值引用的 bean 的约束.

JSF 2.0 doesn't call class level validation constraints. From JSF validation: JSF 2 provides built-in integration with JSR-303 constraints. When you are using bean validation in your application, JSF automatically uses the constraints for beans that are referenced by UIInput values.

此外,答案还建议使用 SeamFaces 来验证类级别的约束.

The answer furthermore suggests using SeamFaces to validate the class-level constraints anyways.

不幸的是,这是一个不可选项,因为它引入了一个稍微庞大的依赖项,用于验证无论如何都应该验证的内容.

Unfortunately this is a non-option, since it introduces a somewhat massive dependency for just validating what should be validated anyways.

我的问题是:

控制器中的手动验证很乏味,而且有很多重复的代码,因此我想避免一个选项.

Manual validation in the controller is tedious and a lot of repeated code, and thus an option I would like to avoid.

我试图通过使用 @Valid 注释要验证的控制器字段来做到这一点,但没有帮助.

I have attempted to do this by annotating the Controller-Field to be validated with @Valid, which didn't help.

应该可以进行流程验证"阶段为我做这件事,或者在更新模型值"之后钩入类似于 Filter 的东西.阶段,这将通过验证集中运行模型值.

I guess it should be possible to either make the "Process Validations" phase do that for me or hook in something akin to a Filter after the "Update Model Values" phase, that would centrally run the model values through a Validation.

推荐答案

在即将推出的 JSF 2.3 之前,JSF 不支持使用 a.o. 的类级别验证.@Valid.这是一个永恒的问题,因为有史以来第一个 JSF 规范问题解决了这个问题.

Until the upcoming JSF 2.3, JSF doesn't support class level validation using a.o. @Valid. This is an eternal issue, given that the very first JSF spec issue ever addresses this.

您的度假村要么使用已经处理过它的 3rd 方库,要么根据相关开源库的来源自制它(考虑到许可).

Your resort is either using a 3rd party library which has already taken care of it, or homebrewing it based on sources of the open source library in question (taking licensing into account).

除了 SeamFaces <s:validateForm> 你已经找到了,还有 OmniFaces <代码>.与 相比的主要区别在于它不使用 JSF Validator,但是一个 JSR303 ConstraintValidator(并且您可以立即掌握整个实体,而无需声明和注释一堆字段,重复实体的属性.

Apart from SeamFaces <s:validateForm> which you already found, there's also OmniFaces <o:validateBean>. The major difference as compared to <s:validateForm> is that it doesn't use a JSF Validator, but a JSR303 ConstraintValidator (and that you've the whole entity immediately at hands without the need to declare and annotate a bunch of fields, repeating the entity's properties.

JSF 2.3 支持将以 <f 的形式出现:validateWholeBean> 主要基于 OmniFaces .

JSF 2.3 support will come in flavor of <f:validateWholeBean> which is largely based on OmniFaces <o:validateBean>.

这篇关于验证 JSF 中的类级别 bean 验证约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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