model-view-controllerjsfjakarta-eejsf-2separation-of-concerns

Best practice for implementing MVC in JSF2 managed beans


As the complexity of the web projects that I am working on increases, the need to include a MVC structure is becoming more urgent. My model classes are well defined, but view and controller code tends to get munged together. I've been using pretty heavy AJAX on the site also (RichFaces jsFunctions mostly) which makes things slightly more complicated.

Has anyone found good strategies for implementing MVC using JSF2? I do not want to introduce another framework to the project (e.g. Spring MVC).

Some ideas thus far, which I haven't started doing yet

Any thoughts?

Edit

What I see pretty often are managed beans that tend to do everything: keep track of users' selections, update the model, get filtered lists, etc...

We're using JSF 1.2 at the moment and so we can't use actions/actionlisteners with parameters. So for example, our managed beans contain variables such as m_selectedDate whose only purpose is to feed the selected date to the back-end on the call to updateFilteredItemsBasedOnDate(). It would be nice if the extra variables could go away since they are just temporary. JSF 2's EL with parameters should help but I'm curious if there is an alternate solution available.

I am curious if there is a way to apply MVC to managed beans, or some method of separating concerns so that we don't end up with large beans that try to do everything.


Solution

  • You have to see the better way of managing the beans rather look into how better use the MVC in JSF. Because JSF itself come with the MVC in better implementation. I am not sure what exactly here you mean as the better way of implementing the MVC.

    As Balusc told in the comments, JSF itself has the sufficient MVC for your application. May be if you want to separate the concerns, it is good idea to separate the business logic into Helper classes and write only the presentation logic in the beans.

    There is debate on whether to use the bean for business logic or should separate it from the managed bean. That is all depends on the your application and domain specific.

    Thanks, Krishna