Kishor Prakash
Kishor Prakash

Reputation: 8171

Dynamic rendering of ui:include won't work when ui:repeat is used in included facelet

Hello,

I'm putting ui:include inside h:panelGroup and rendering it dynamically when required.

The usual assumption and theory is, ManagedBean associated with the included page should not get initialized when ui:include is not rendered.

This is not happening when I use ui:repeat inside included page.

Here is a Example to repeat the Issue:

page.1.xhtml

<h:body>
    <h:form>
        <p:outputLabel value="#{mBeanOne.beanOnetxt}"/><br/>
    </h:form>

    <h:panelGroup rendered="false" layout="block">
        <ui:include src="page2.xhtml"/>
    </h:panelGroup>
</h:body>

MBeanOne.java

@ManagedBean
@ViewScoped
public class MBeanOne implements Serializable{

    private String beanOnetxt;

    public MBeanOne() {
        System.out.println("MBeanOne - Constructor");
    }

    @PostConstruct
    public void init(){
        beanOnetxt = "This is Managed Bean 1";
        System.out.println("MBeanOne - Postconstruct");
    }
    //SETTERS and GETTERS
}

page2.xhtml

<h:body>
    <h:outputText value="#{mBeanTwo.beanTwotxt}"/><br/>

    <ui:repeat var="var" value="#{mBeanTwo.versionList}">
        <h:outputText value="#{var}"/><br/>
    </ui:repeat>
</h:body>

MBeanTwo.java

@ManagedBean
@ViewScoped
public class MBeanTwo implements Serializable{

    private String beanTwotxt;
    private List<String> versionList;

    public MBeanTwo() {
        System.out.println("MBeanTwo - Constructor");
    }

    @PostConstruct
    public void init(){
        beanTwotxt = "This is Managed Bean 2";

        versionList = new ArrayList<String>();
        versionList.add("Playground");
        versionList.add("Kestrel");
        versionList.add("Merlin");
        versionList.add("Tiger");

        System.out.println("MBeanTwo - Postconstruct");
    }
    //SETTER and GETTER
}

As you can see in page1.xhtml I have use rendered="false" for h:paneGroup which encapsulates ui:include, but still MBeanTwo is getting initialized when I load page1.xhtml.

If I remove ui:repeat from page2.xhtml then MBeanTwo is not getting initialized.

Is this a bug in JSF, if so any workaround available?

Versions used and tested:
Primefaces 3.5
JSF 2.1.13 and 2.2.0-m05

Upvotes: 3

Views: 1459

Answers (1)

Aritz
Aritz

Reputation: 31679

The <ui:include /> piece is not relevant in your question, it could be everything done in the same view with identical behaviour.

Anyway, it seems that <ui:repeat /> tag doesn't check its parents rendered attributes before doing bean initialization. However, the mBeanTwo#getVersionList method never gets evaluated, just the managed bean is created. That could bring you to a slowdown if you perform data access when you construct your bean, but never should break your view. I'm able to reproduce the problem in Mojarra 2.1.x versions, however they have fixed it for 2.2.x latest minors.

Seems a Mojarra implementation bug rather than an expected behaviour.

See also:

Upvotes: 1

Related Questions