Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Server Faces: Validation only in business logic tier

Tags:

validation

jsf

I have an Java Server Faces web application and I am unsure how to handle the validation.

In my opinion the validation should be done in the bussiness logic tier. The business logic layer is used inside the web presentation (jsf) and REST-API.

At the moment i am doing also the validation inside the jsf layer with the provided validators. I think that is in the most cases only duplication of code. Is there any way to avoid this code duplication? Is java server faces able to use the validation exceptions that i am throwing inside the business logic layer?

like image 991
Marcello90 Avatar asked Dec 13 '13 04:12

Marcello90


1 Answers

Bean Validation has been invented for exactly this case.

You annotate your entities with constraints, and these constraints will be honoured by both your business logic (via EJB, CDI and/or JPA) as well as by JSF.

For the small amount of validations that you can't express via Bean Validation but are truly business associated; yes, throw an exception, catch it in your backing bean and set a corresponding Faces message (hint: use OmniFaces' Messages to make this easier). Equally, for the small amount of validations that you can't express with Bean Validation and which are strongly view oriented; use JSF native validators.

like image 97
Mike Braun Avatar answered Oct 21 '22 14:10

Mike Braun