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

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

问题描述

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

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

答案还建议无论如何都要使用SeamFaces来验证类级别的约束.

不幸的是,这不是一个选择,因为它引入了某种程度的依赖性,以仅仅验证应验证的内容.

我的问题是:

如何让JSF验证类级别的约束?

在控制器中进行手动验证非常繁琐,并且需要重复很多代码,因此我想避免这种选择.

我试图通过注释要用@Valid进行验证的Controller-Field来做到这一点,但这没有帮助.

猜测,应该有可能使过程验证"阶段对我有用,或者在更新模型值"阶段之后加入类似于Filter的东西,这可能通过验证集中运行模型值.

解决方案

在即将发布的JSF 2.3之前,JSF不支持使用a.o进行类级别的验证. @Valid.鉴于有史以来第一次JSF规范问题都解决了这个问题. /p>

您的度假村正在使用已经处理过的第三方库,或者根据相关开源库的来源(考虑许可)对其进行自制.

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

JSF 2.3支持将以 <f:validateWholeBean> 主要基于OmniFaces <o:validateBean>.

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

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.

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.

My question thus is:

How can I get JSF to validate class-level constraints?

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

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

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.

解决方案

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.

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

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 support will come in flavor of <f:validateWholeBean> which is largely based on OmniFaces <o:validateBean>.

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

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