Java server faces: validate only in the business logic layer
I have a java server faces web application and I'm not sure how to handle validation
In my opinion, validation should be done at the business logic layer The business logic layer is used for web presentation (JSF) and rest - API
At present, I am using the provided verifier to verify in the JSF layer I think in most cases, it will only repeat the code Is there any way to avoid this code duplication? Can the java server use the validation exceptions I throw in the business logic layer?
Solution
Bean verification is invented for this situation
You annotate your entities with constraints that will be respected by your business logic (through EJB, CDI and / or JPA) and JSF
For a small amount of validation, you cannot validate the expression through bean, but it is really business-related; Yes, throw an exception, catch it in your supporting bean and set the corresponding faces messages (hint: use omnifaces' messages to make this easier) Similarly, for a small amount of validation, you can't use bean to validate the expression, and it is strongly oriented to the point of view; Use JSF native validator