Question

I have implemented a composite component in JSF using primefaces.

<ui:component ...>
    <cc:interface>
            <cc:facet name="header"/> ...
    <cc:interface>

<cc:implementation>
   <p:dataTable>
    <f:facet name="header">
       <c:choose>
         <c:when test="#{empty cc.attrs.metadata.headerText}">
            <cc:insertFacet name="header" required="true"/>
        </c:when>
        <c:otherwise>
        #{cc.attrs.headerText}
        </c:otherwise>
       </c:choose>
    </f:facet> ...
   </dataTable>
</cc:implementation>

When I use it in a normal page it works fine as expected rendering the datatable.

<ui:composition>
    <nav:dataTable/>
        <f:facet name="header">
            <h:outputText value="headerText" />
        </f:facet>
</ui:composition>

But when I use it inside a dialog which uses the above composite component, it throws

component.xhtml @28,54

Unable to find facet named 'header' in parent composite component with id 'j_idt129'

I am making a ajax call to invoke this dialog on click of a link. The dialog comes in different shape and throwing this error in console. Has anyone faced it ? Any help is really appreciable.

Was it helpful?

Solution

cc:insertFacet inserts the whole f:facet tag, so you shouldn't wrap it into another f:facet tag within the composite implementation. As you are writing a custom p:dataTable I think it's easier to overwrite the already existing header facelet as an interface declaration and conditionally render it using JSTL utilities:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<h:body>
    <composite:interface>
        <composite:facet name="header" />
        <composite:attribute name="title" />
    </composite:interface>

    <composite:implementation>
        <p:dataTable>
            <!-- If the facet is given at parent, insert it. 
            Otherwise, provide the title given by the attribute -->
            <c:choose>
                <c:when test="#{not empty cc.facets.header}">
                    <composite:insertFacet name="header" />
                </c:when>
                <c:otherwise>
                    <f:facet name="header">
                        #{cc.attrs.title}
                    </f:facet>
                </c:otherwise>
            </c:choose>

            <p:column headerText="column" />
        </p:dataTable>
    </composite:implementation>
</h:body>
</html>

Using it as:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:comp="http://java.sun.com/jsf/composite/comp">

<f:view>
    <h:head />
    <h:body>
        <h:form>
            <comp:myTable title="header">
                <f:facet name="header">
                    <h:outputText value="text" />
                </f:facet>
            </comp:myTable>

            <comp:myTable title="Custom header" />

            <p:commandButton type="button" onclick="dialog.show()"
                value="show in dialog" />

            <p:dialog widgetVar="dialog">
                <comp:myTable title="header">
                    <f:facet name="header">
                        <h:outputText value="text" />
                    </f:facet>
                </comp:myTable>
            </p:dialog>

        </h:form>
    </h:body>
</f:view>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top