Jsf 2.0- <ui:include> xhtml总是包含,即使渲染是假的

Nee*_*cks 2 xhtml jsf jstl jsf-2 uiinclude

我有一个主页xhtml,我根据条件包括3个孩子xhtml.我面临的问题是,无论情况如何,Book.xhtml总是被调用.我将渲染条件更改为false或移出到另一个条件,但始终调用该文件由于其调用其支持bean而导致不必要的开销.请给我一个解决方案

<ui:composition template="/xhtml/baseLayout.xhtml">
    <ui:define name="browserTitle">
        <h:outputText value="HOME PAGE" />
    </ui:define>
    <ui:define name="header">
        <ui:include src="/xhtml/header.xhtml" />
    </ui:define>
    <ui:define name="bodyContent">

        <h:panelGrid width="100%"
            rendered="#{pogcore:isRoleAuthorized(BUNDLE.SUPER)}"  >
            <ui:include src="/xhtml/SuperUser.xhtml"  />
        </h:panelGrid>
        <h:panelGrid width="100%"
            rendered="#{pogcore:isRoleAuthorized(BUNDLE.MAINTENANCE)}" >
            <ui:include src="/xhtml/Maintenance.xhtml" />
        </h:panelGrid>

        <h:panelGrid width="100%"
            rendered="#{pogcore:isRoleAuthorized(BUNDLE.PRINT)}">
            <ui:include src="/xhtml/Book.xhtml" />
        </h:panelGrid>

    </ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)

cub*_*buk 13

这是由于jsf的生命周期而发生的.在视图渲染时评估JSF UIComponent,因为在构建时评估jstl标记.

因此,当您使用h:panelGrid的呈现属性时,在包含的页面下不调用托管bean为时已晚.要解决这个尝试使用jstl标记的条件,以下应该适合您.

<c:if test="#{bean.yourCondition}">
    <h:panelGrid width="100%"> 
        <h:outputText value="#{bean.yourCondition}"/> <!--if this is not getting printed there is smtg wrong with your condition, ensure the syntax, the method signature is correct-->
        <ui:include src="/xhtml/Book.xhtml" /> 
    </h:panelGrid>
</c:if> 
<c:if test="#{!bean.yourCondition}"> 
    <h:outputText value="#{bean.yourCondition}"/> <!--This should print false-->
</c:if>
Run Code Online (Sandbox Code Playgroud)

下面的文档描述了jstl和jsf生命周期的细节.

http://www.znetdevelopment.com/blogs/2008/10/18/jstl-with-jsffacelets/

请查看以下文档,了解在不使用jstl标记的情况下解决此问题的另一种方法.

http://pilhuhn.blogspot.com/2009/12/facelets-uiinclude-considered-powerful.html