In the previous Facelets Tutorial we have shown a simple facelet example that demonstrates the use of templates to promote code reuse. In this tutorial we will show how to create composition components to create extensible components.

A composite component is a special type of template that acts as a component.

In practive, a composite component consists of a collection of markup tags and other existing components. This reusable, user-created component has a customized, defined functionality and can have validators, converters, and listeners attached to it like any other component.

Recall our templateClient.xhtml from the previous Facelets Tutorial. Let's suppose we want to insert into the Content section a picure and a PanelGrid with a variable number of columns/rows.

Let's build first our customized component, by creating a page named panel.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
 <h:graphicImage value="#{resource['']}"/>     

 <h:panelGrid columns="${cols}" border="1" >
 <f:facet name="header">
   <h:outputText value="${title}"/>
  <c:forEach var="field" items="${data}">
   <h:outputText value="#{field}"/>

This page, renders an image and a panelGrid. The relevant portion of the codes are the parameters of this page which are:

  • cols: the number of columns in the panel
  • title: the title of the panel
  • data: the collection containing the data.

Now you must register this composite component in an xml file much the same way you did in the past for JSP custom tags


Now save this file in the WEB-INF folder of your Web application as sampletaglib.xml.

The last configuration step, will be adding in your faces-config.xml a parameter which defines where facelets libraries are located (in our example, under WEB-INF/sampletaglib.xml).


That's all. Now if you want to reference your composite tag in your facelets, just declare the xml namespace in the html section (in this example we will reference it as "i"):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""
  <ui:composition template="./template.xhtml">

    <ui:define name="content">
      <i:mypanel cols="3" title="Title of the panel" data="#{personBean.fields}" />


Notice we have bound the parameters cols, title and data which we have declared in our pane.xhtml template page.

Finally, we need to define the class PersonBean, which is tagged using JSF 2 annotations

package sample;


public class PersonBean {
 private ArrayList fields = new ArrayList();
 public PersonBean() {
  fields.add(new String("Column1"));
  fields.add(new String("Column2"));
  fields.add(new String("Column3"));
  public ArrayList getFields(){
   return fields;

The composite component when rendered will generate something like this:

Title of the panel
Column1 Column2 Column3