Best practice for implementing MVC in managed JSF2 beans

As the complexity of the web projects I'm working on is increasing, the need to include an MVC framework is becoming increasingly relevant. My model classes are well defined, but view and controller code tend to combine. I also used the pretty heavy AJAX on the site (especially in RichFaces jsFunctions), which makes things a little more complicated.

Has anyone found good strategies for implementing MVC using JSF2? I do not want to introduce another structure into the project (for example, Spring MVC).

Some ideas so far that I have not yet begun to do

  • For a page with heavy ajax, find the “view” of the bean to remember the selected tabs, selected items, provide filtered data lists, etc.
  • Have a bean to handle actions like model changes
  • Have the 'beans' command that goes between the JSF page and the controller. The jsFunction function populates the bean command with parameters, and calling command.execute() calls the bean command to call the correct method on the bean controller to complete the action. The bean "command" may include some javascript that will be called upon completion. It can also indicate regions of a page for re-rendering.

Any thoughts?

Edit

I see quite often managed beans that tend to do everything : track user choices, update the model, get filtered lists, etc.

We are currently using JSF 1.2, so we cannot use actions / actionlisteners with parameters. For example, our managed beans contains variables, such as m_selectedDate , whose sole purpose is to pass the selected date to the background code when updateFilteredItemsBasedOnDate() called. It would be nice if the extra variables could disappear as they are temporary. JSF 2 EL with parameters should help, but I'm curious if there is an alternative solution.

I am curious if there is a way to apply MVC to managed beans or to some method of sharing problems, so that we don’t get big beans that tries to do everything.

+4
source share
3 answers

You should see a better way to manage beans, take a quick look at how best to use MVC in JSF. Because JSF itself comes with MVC in a better implementation. I'm not sure what exactly here you mean as the best way to implement MVC.

As Balusc said in the comments, JSF has enough MVC for your application. Maybe if you want to separate problems, it is advisable to separate business logic into helper classes and write only presentation logic in beans.

There is discussion about whether to use a bean for business logic or to separate it from a managed bean. It all depends on your application and specific area.

Thank you Krsna

0
source

Are you looking for design templates? If you are looking for MVC, then the BalusC answer is enough, otherwise check it out

http://www.allapplabs.com/j2ee_design_patterns/j2ee_design_patterns.htm

Also check this

+3
source

Well, maybe you need a layered architecture. So far MVC architecure, there are more problems. For example, you can overlay the architecure layer on the View, Business Logic, and DataAccess levels, implementing most of the projects of creating common interest and corporate templates at each level.

You started by saying that one of them implements MVC, but it’s good practice to consider “separation of problems”

This way you can achieve well-organized and decoupled code.

I would like this brief explanation to be useful.

Sincerely.

+1
source

All Articles