Skip to main content

best-practice identical pages and managed beans

3 replies [Last post]
Joined: 2010-05-12


I have a best-practice-question for following requirement:

Imagine, your JSF application has to be devided in 3 main tabs. The business logic of the 3 pages is almost the same, the GUI Layout differs a bit, each tab "has its own state", which must not be lost when changing the tabs.

The GUI consists of 3 three tabs, each displaying soccer results:
Premiere League, Serie A, Primera Division
The page showResults.xhtml is responsible for displaying the results.
The ManagedBean SoccerBean holds the data of the soccer results.

Problem: I want to avoid redundancies. But:

I have to duplicate showResults.xhtml
because each page must reference its own ManagedBean instance to preserve its own state:

showResultsPremiereLeague.xhtml --> uses #{soccerBeanPremiereLeague} of type PrimierLeagueSoccerBean
showResultsSerieA.xhtml --> uses #{soccerBeanSerieA} of type SeriaASoccerBean
showResultsPrimeraDivision.xhtml --> uses #{soccerBeanPrimeraDivision} of type PrimeraDivisionSoccerBean

OK, regarding the managed bean, you can create a PrimeraDivisionSoccerBean SeriaASoccerBean PrimierLeagueSoccerBean to have soccer league specific business logic.

But the problem is, that I have many redundancies when I want to access the managed bean via EL (for example in xhtml pages, converters, other managed beans).

What is best practice for this case?

Implementing a custom EL resolver, which can resolve #{soccerBean} to the wanted bean (in this case the el resolver needs to know the current active tab) so that in the PremierLeague #{soccerBean} can resolve to #{soccerBeanPremiereLeague} ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-08-16

I've dealt with this exact issue by using the Tomahawk tag t:aliasBean. With this each of your showResults*.xhtml pages will set the alias for the real controller and pass this alias to the included "common content" page.

Joined: 2010-05-07

Hi, i used too to use t:aliasBean and jsp.include to replicate the layout of a page and behviour, or with JSF2 i passed to use Facelets couse is standarized and tomhawk is not supported still in jsf2.
I would create a template page showResults.xhtml where inside i use a variable #{soccerBean}. Than in each tab i would include the page in this way:

Than for specific page contents, inside the tab u can inser content using ui:define, and in the template pag eu can specify where insert contents using ui:insert. See javadocs for more details.

Regards, Daniele

Joined: 2010-05-13

Regarding the business logic, if you think almost everything is same, then have a pojo which handles all the business logic and use the pojo in the Beans. So that, the beans will contain only the presentation data. And also, you can make another layer like BO layer to accomodate such business logic pojo classes to talk to the Model. It's easy for future enhancements and maintanance as well.

For usage in the screens, you can configure an application level managed bean and other beans as the attributes of this bean. To inject these with out loosing the state, you can configure them in faces-config.xml. I think you might be knowing how to do this.