Conditionally including a Facelet file via <ui:include>

拟墨画扇 提交于 2019-12-04 05:51:40

问题


I have 2 Facelets files (index.xhtml and report.xhtml). I use the RichFaces <ui:include src="report.xhtml"/> to load the report into the index. Works fine. But when I try to only show report on certain conditions, I fail! What does not work is this:

<ui:include src="report.xhtml" rendered="#{indexService.contentName == 'report'}"/>.

The rendered attribute of ui:include does not seem to work.

How can I load the report.xhtml into the index.xhtml on certain conditions? Where is the error in my code?

Edit:

Half of it works now. Changing the Facelet file works with conditions. But the functionality of the loaded Facelet does not work properly. Why is that? Where is the problem in the code?

Based on the suggestions I now have this:

<h:form>
    <h:panelGrid>
        <a4j:commandLink value="Home" render="contentpanel" action="#{indexService.setContentName('home')}"/>
        <a4j:commandLink value="Report" render="contentpanel" action="#{indexService.setContentName('report')}"/>
    </h:panelGrid>
</h:form>
<a4j:outputPanel id="contentpanel">
    <ui:fragment rendered="#{indexService.contentName eq 'report'}">
        <ui:include src="report.xhtml" />
    </ui:fragment>
</a4j:outputPanel>

Edit 2:

This is my report Facelet. If I use without any condition the functionality of the report Facelet works perfectly, but if I load it using the condition I posted in Edit 1, then the buttons of <rich:panelMenuItem .../> don't work anymore and <h:outputText escape="false" value="#{reportService.content}"/> does not load the content. Any idea why?

Edit 3:

Changed the <rich:panel header="Report">...</rich:panel>, but behaviour still unchanged.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j">
    <ui:composition>
        <h:outputStylesheet>
            .placesmenu {
                width: 200px;
                vertical-align: top;
            }

            .contentplace {
                vertical-align: top;
            }
        </h:outputStylesheet>
        <h:panelGrid columns="2" width="100%" columnClasses="placesmenu, contentplace">
            <rich:panel header="Places">
                <h:form>
                    <rich:panelMenu style="width: 170px">
                        <a4j:repeat value="#{reportService.menuItems}" var="menuItem" id="repeat_layer1">
                            <rich:panelMenuGroup label="#{menuItem.label}">
                                <a4j:repeat value="#{menuItem.subMenuItemList}" var="subMenuItem" id="repeat_layer2">
                                    <rich:panelMenuItem label="#{subMenuItem.label}" render="reportpanel" onbeforedomupdate="#{reportService.setId(subMenuItem.id)}"/>
                                </a4j:repeat>
                            </rich:panelMenuGroup>
                        </a4j:repeat>
                    </rich:panelMenu>
                </h:form>
            </rich:panel>
            <rich:panel header="Report">
                <h:outputText escape="false" value="#{reportService.content}" id="reportpanel"/>
            </rich:panel>
        </h:panelGrid>
    </ui:composition>
</html>

回答1:


try to surround your ui:include tag with ui:fragment as follows :-

<ui:fragment rendered="#{indexService.contentName eq 'report'}">
     <ui:include src="report.xhtml" /> 
</ui:fragment>


来源:https://stackoverflow.com/questions/26405708/conditionally-including-a-facelet-file-via-uiinclude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!