Reputation: 23443
JSF 2.0 has offered Bean Validation
(JSR 303) and its own Validation framework
, given the choices, i am confused with which one to choose from.
It seems that with JSF's validation framework, i may specify constraints at the XHTML view layer. i.e. max length. While using Bean Validation, i would specify the constraints via annotations (@) in the bean.
I see the JSF Validation framework as more of a plug-gable attempt, while the bean validation JSR as an elegant solution, can anyone comment on this?
May i check if anyone has used one over another and why, or if anyone has used both together?
Upvotes: 1
Views: 1183
Reputation: 1
Bean Validation at model makes more sense, if you want to promote extensibility of the project, say for example you want to expose some of the entity service outside, [ eg for a CRUD
operation ).
Upvotes: 0
Reputation: 1109512
First of all, Bean Validation is not part of JSF. It's part of Java EE, which JSF in turn is also part of. Bean Validation is also useable on non-JSF applications. Bean Validation is just designed to provide a Java EE standard for validation frameworks.
The major functional difference between those validation approaches is that JSF own validation is controlled at view level and that Bean Validation is controlled at model level. Further, there's a technical difference: Bean Validation is not necessarily supported out the box on all containers. Simple servletcontainers like Tomcat for example don't support it. You'd need to supply the Bean Validation API/impl with the webapp yourself.
If you're using models where you have no control over, then you'd need to make the decision to wrap them in another model in order to utilize Bean Validation, or to define the validation in the JSF views. Which to choose depends on the functional and technical requirements. Both works equally good.
If you're using models where you have full control over and they are also reused for other views than JSF and the validation constraints depend fully on the database datamodels, then Bean Validation is a better option than JSF own validation as it minimizes code repetition on validation constraints.
Upvotes: 3