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

For a page with heavy ajax, have a 'view' bean for remembering selected tabs, selected items, providing filtered lists of data, etc...

Have a 'controller' bean to handle actions such as changes to the model

Have 'command' beans which go between the JSF page and controller. A jsFunction populates the command bean with parameters, and calling command.execute() causes the command bean to call the correct method on the controller bean to perform the action. The 'command' bean may include some javascript to be called on completion. It may also specify regions of the page to be re-rendered.

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.

I fail to see the point of "How do I MVC with JSF?". JSF itself is already a MVC(2) framework. The managed beans are the M, the facelets pages are the V, the builtin FacesServlet is the C. Isn't your question rather "How do I design the managed beans in JSF?".
–
BalusCNov 19 '10 at 21:27

Good point. I updated the question title and added some comments.
–
JonNov 19 '10 at 21:44

3 Answers
3

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.

Well, maybe what you need is a layered architecture. So far a MVC architecure, there are more concerns.
For example, you could layer you architecure in View, Business Logic and DataAccess layers, implemeting most of commons design and enterprise patterns in each layer.

You h've started whit one of them implementing MVC, but, is a good practice take in count "separation of concerns"