1
votes

Iam about to create a webapplication with JavaServer Faces 2. In the backend things are managed with other usual JEE technologies like EJB3.1 and JPA2. The point is, Iam following some domain driven architecture, which means, the the domain models are used at the JSF layer as backing bean models and are typically persisted as persistent entities at the persistence layer. Using the same model at different layers yields the advantage of only defining the related model restrictions once and for all. Using Bean Validation at this level provides the restrictions of this model to either JSF, JPA etc.

Now my question is, whether using bean validation with JSF2 is a good idea? My concerns are that linking the validation restrictions directly to the model might be the wrong approach, as the validation of the JSF lifecycle usually happens somehow earlier than accessing the model (for its validations rules). As much as I know JSF validation is not taking place during model processing (aka. phase 4: apply model values) but earlier in its own dedicated point in time (phase 3: process validations) and is applied on the component value (submitted value). However, how should the JSF validation (in phase 3) know the actual restrictions of the bean, if it is not validating during the model processing?

Thanks for any clarification on this.

[Edit] To be more specific, right now Iam using Bean Validation for this purpose and it is working, as invalid values are rejected and a faces message is properly shown. I just wonder if it is the right approach, as to my understanding the validation might take place in the wrong JSF phase and thus might lead to an inconsist component model.

1
How exactly did you conclude/observe that bean validation is executed during update model values phase instead of validations phase? Or is this just a blind assumption without actually having tested/debugged it yourself?BalusC
It is a blind assumption, based on the fact that it is the model wherein the validation rules are defined (due to bean validation). With your comment now Iam curious whether the JSF impl is able to extract or refer to these rules from the model and apply them during the validation phase on the component value..Lars
Used your approach (balusc.blogspot.de/2006/09/debug-jsf-lifecycle.html) to verify which phases were invoked: RESTORE_VIEW(1) -> APPLY_REQUEST_VALUES(2) -> PROCESS_VALIDATIONS(3) -> RENDER_RESPONSE(6) So in fact it was a misconception that the APPLY_MODEL_VALUES(4) phase was invoked. Thanks for the hint.Lars

1 Answers

-1
votes

You can add all those bean validations in Data Transfer Object(DTO), The DTO's responsible for delevering the UI data in the JSF. After all the validations are succeed you can copy the DTO's to the Entity(Model) Object.

For copying the (DTO's to Entity) or (Entity to DTO's) we can use third party librires like dozer mapper.

This will avoid the validation restrictions directly to the model layer. I mean the Entity Objects