So I've developed a JSF based application which has a navigation menu (PrimeFaces menu) and a central content area, this content area is dynamic and it refreshes based upon the active element of the navigation menu. So imagine something like the following.
<ui:composition template="/WEB-INF/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="nav">
Navigation menu with items
</ui:define>
<ui:define name="content">
<ui:include src="#{pageManager.currentPage}.xhtml"/>
</ui:define>
</ui:composition>
Each content section (all ajax based) has it's own model and it's own @ViewScoped
bean, each one of those controller beans will utilise a number of CDI beans to do CRUD type operations with the model. These beans are mostly only enterprise @RequestScoped
.
This all works fine. My question is because technically there is never any navigation as such (the content div is updated, there is no navigation from page1->page2 etc.) I believe all of my controller beans remain in the viewscope whether that content is active or not, also I think the CDI beans injected into them are also effectively promoted to the view scope. Is this correct?
If so is there a better approach, given that it's only the content section of the page which changes based upon the active menu selection?
As long as you need state and cannot use RequestScope for some of the beans the behavior will be exactly what you described.
If you don't have an issue with the memory size that you use in the ViewScoped beans you don't have to worry about.