Validate class level bean validation constraints in JSF

寵の児 提交于 2019-11-26 14:56:00

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/27781202/validate-class-level-bean-validation-constraints-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!