JSF 2 Composite Components Example | Create Custom Tag

By Arvind Rai, January 23, 2015

In this page we will learn creating composite component in JSF 2 with the help of composite:interface, composite:attribute and composite:implementation tag. JSF 2 allows to create template which can be reused where ever we want. To do it, create a directory named as resources parallel to WEB-INF. Inside resources directory, create a folder as we want and then create template file . The name of our template file will be our custom tag name.

Project Structure in Eclipse

Create Template with Composite Interface and Implementation

We will create a template with the help of composite:interface, composite:attribute and composite:implementation. Perform some basic task.

1. Create a directory named as resources parallel to WEB-INF
2. Create your own directory and inside it create your own file and import xmlns:composite="http://java.sun.com/jsf/composite

For the example, if we create template file as Resources/concretepage/ userform.xhtml then to use it, we will import it any JSF file as xmlns:concretepage = "http://java.sun.com/jsf/composite/concretepage"
Here our own tag is concretepage and can be used in any JSF file as &ltconcretepage /&gt. We can define its attribute using tag composite:interface which defines the name of tag attribute using sub tag composite:attribute . Now create the template using composite:implementation tag. Find the template file.
userform.xhtml

Define composite:attribute in composite:implementation by using cc.attrs. For the attribute which defines the action, we have to define method signature using method-signature attribute of composite:attribute tag.

Use Template

To use our template, we need to import as xmlns:concretepage = "http://java.sun.com/jsf/composite/concretepage"
Now we have a new tag which has the attribute defined in userform.xhtml.
user.xhtml