What does <f:facet> do and when should I use it?

hello123 picture hello123 · Aug 6, 2014 · Viewed 60.7k times · Source

I have been having trouble with the tag <f:facet>. I am working form other examples of code which use it, but I'm not sure exactly what purpose it serves.

I have written some code which in method is exactly the same as other code I have seen which works, except there's is wrapped in a <f:facet name=actions> tag. When I add this around my code the drop down box I am wrapping it around disappears when I deploy. Anyone able to suggest a reason for this or give me an insight into how and when to use facet?

Here is my code, I won't bother adding the bean code as they're just basic getters and setters and I don't think they're causing the trouble.

<f:facet name="actions">
    <p:selectOneMenu id="SwitchWeekDrpDwnMenu" 
                     value="#{depotWorkloadBean.selectView}"
                     partialSubmit="true">
        <p:ajax update="mainForm" 
                listener="#{depotWorkloadBean.updateView}" />
        <f:selectItem itemLabel="Day view" itemValue="Day"/>
        <f:selectItem itemLabel="01/01/2014" itemValue="Week"/>
    </p:selectOneMenu>
</f:facet>

If I remove the facet tag the dropdown box displays, but doesn't function as it should with the beans.

Answer

Flowy picture Flowy · Jan 10, 2016

A facet represents a named section within a container component. For example, you can create a header and a footer facet for a dataTable component. https://web.archive.org/web/20170828020413/http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_facet.html

It's useful when you want to create component that uses some code from user (let's say wrapper).

ie. when you want to create component that hides long text and shows short version of it. You can use just the element body, but then you will get only one value, if you want to get from user the short AND the long version then you can not do it in one value (without using some discriminant), just use facet and say which one is the long and which is the short version.

<textShortener>
    <f:facet name="short">
        This text is short.
    </f:facet>
    <f:facet name="long">
        This text is too <b>long</b> to be showed when page loads. User have to click the button after the short text to show this.
    </f:facet>
</textShortener>

Yes, this can (and should) be done with jsf templating, but I hope you got it.

To question: you defined facet just in the wild xml, nobody requested it so nobody processed it - that's why it did not throw error nor showed anything.