Java Server Faces: Validation Only at the Business Logic Level

I have a Java Server Faces web application and am not sure how to handle validation.

In my opinion, validation should be done at the logical level of the business. The business logic layer is used internally in web presentation (jsf) and REST-API.

At the moment, I am also doing validation inside the jsf layer with the validators provided. I think that in most cases this is just code duplication. Is there a way to avoid code duplication? Is the java server able to use validation exceptions that I throw inside the business logic layer?

+7
validation jsf
source share
1 answer

Bean Validation was invented just for this occasion.

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

For a small number of checks that you cannot express using Bean Validation, but are really business related; yes, throw an exception, catch it in your Bean support and set the appropriate Faces message (hint: use OmniFaces messages to make it easier). Similarly, for a small number of checks that you cannot express using Bean Validation and which are highly view-oriented; Use built-in JSF validators.

+5
source share

All Articles