Facelets tutorial. Using composition components
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:
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"):
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
The composite component when rendered will generate something like this:
| Title of the panel | ||
|---|---|---|
| Column1 | Column2 | Column3 |
References: http://download.oracle.com/javaee/6/tutorial/doc/giqzr.html

