How to lazy Load ManagedBean associated with the included page in p: Tab, Only when opening a tab

I have p:tabViewor p:accordionPaneland Facelets included in each p:tabusing ui:include.
My problem is the ManagedBeans associated with each included page is initialized at startup itself. How can I make them Initialize only when I open a specific tab.
Here is a sample code: index.xhtml

<p:tabView dynamic="true" cache="true">
   <p:tab title="Bean 1 Page 1">
    <ui:include src="page1.xhtml"/>
   </p:tab>

   <p:tab title="Bean 2 Page 2">
    <ui:include src="page2.xhtml"/>
   </p:tab>
</p:tabView>

page1.xhtml

<h:body>
    <f:metadata>
    <f:event listener="#{bean1.bean1PreRender}" type="preRenderView"/>
    </f:metadata>
    <h:form>
    <h:outputLabel value="#{bean1.bean1Text}"/>
    </h:form>
</h:body>

Bean1.java

@ManagedBean
@ViewScoped
public class Bean1 implements Serializable{
    private String bean1Text = "Hello From Bean 1";
    public Bean1() {
        System.out.println("Bean 1 Constructor");
    }
    @PostConstruct
    public void init(){
        System.out.println("Bean 1 @PostConstruct");
    }
    public void bean1PreRender(){
            System.out.println("Bean 1 PreRender PostBack Call");
        if(!FacesContext.getCurrentInstance().isPostback()){
            System.out.println("Bean 1 PreRender NON PostBack Call");
        }
    }
    //SETTER GETTER
}

page2.xhtml

<h:body>
    <f:metadata>
    <f:event listener="#{bean2.bean2PreRender}" type="preRenderView"/>
    </f:metadata>
    <h:form>
    <h:outputLabel value="#{bean2.bean2Text}"/>
    </h:form>
</h:body>

Bean2.java

    @ManagedBean
    @ViewScoped
    public class Bean2 implements Serializable{
        private String bean2Text = "Hello From Bean 2";
        public Bean2() {
            System.out.println("Bean 2 Constructor");
        }
        @PostConstruct
        public void init(){
            System.out.println("Bean 2 @PostConstruct");
        }
        public void bean2PreRender(){
                System.out.println("Bean 2 PreRender PostBack Call");
            if(!FacesContext.getCurrentInstance().isPostback()){
                System.out.println("Bean 2 PreRender NON PostBack Call");
            }
        }
        //SETTER GETTER
    }

}

#{bean1} #{bean2} index.xhtml.
- Tab1, , #{bean1}, #{bean2}?
, , - , , , , .

* : Primfaces 3.5 JSF 2. * 1

+4
1

, , , , , ( <p:tabView>)? ( dynamic="true"):

<h:form>  
    <p:accordionPanel dynamic="true" cache="true">  
        <p:tab title="Bean 1 Page 1">
            <ui:include src="page1.xhtml"/>
        </p:tab>
        <p:tab title="Bean 2 Page 2">
            <ui:include src="page2.xhtml"/>
        </p:tab>
    </p:accordionPanel>
</h:form>

, , JSF , , beans . , :

<h:panelGroup rendered="false">
    <ui:include src="page1.xhtml"/>
</h:panelGroup>

JSF ( ), HTML DOM ( ) , , , (<ui:include> taghandler) , ( rendered). , , <c:if>, <ui:include> :

<c:if test="#{bean.tab eq 1}">
    <ui:include src="page1.xhtml"/>
</c:if>

, PrimeFaces <p:accordionPanel> , , ( ), , <c:if>. :

<h:form>  
    <p:accordionPanel dynamic="true" cache="true">
        <p:tab title="Bean 1 Page 1">
            <c:if test="#{component.parent.activeIndex eq 0 or empty component.parent.activeIndex}">
                <ui:include src="page1.xhtml"/>
            </c:if>
        </p:tab>
        <p:tab title="Bean 2 Page 2">
            <c:if test="#{component.parent.activeIndex eq 1}">
                <ui:include src="page2.xhtml"/>
            </c:if>
        </p:tab>
    </p:accordionPanel>
</h:form>
+1

All Articles