JSF 2 Custom Tags Example

You’ve already seen how to lay out user interface elements with templates. In addition, facelets allows you to define a custom tags. A custom tag looks like a regular JSF tag, but it uses the facelets composition mechanism to insert content into your page.

Implementing a custom Facelets tags with jsf2.0 is a two step:

Implement the custom tag (or component) in an XHTML file.

Declare the custom tag in a tag library descriptor.

Implementing custom facelets tags with jsf2.0 is a simple matter, and it is highly recommended for factoring out repetitive markup. Even that custom facelets tags aren’t as powerful as a full-fledged JSF components (It will be discussed later). In particular, you cannot attach functionality, such as validators or listeners, to facelets custom tag. As an example we cannot add an action listener to the javabeat:tutorial tag. Jsf2.0 addresses this concern with a more advanced component mechanism, called composite components. Composite components will be discussed in a separate post.

5. Managed Bean

The using of custom component doesn’t require using of managed beans, although the limitations of using custom tags is exist, but it doesn’t mean that the managed beans couldn’t be used.TutorialNavigator.java

6. JSF 2 Custom Tag Demo

The below snapshots will show you complete demonstration of using a custom tag for making a links that can help navigating between different tutorials.

One remaining note must be mentioned, in that the possibility of using a ui:component as an alternative way of making a custom Tag. Using of ui:component tag will make the child elements to be placed inside a jsf component view, so the created custom tag is applicable to use binding & rendered attributes in order the component can be associated into a bean property and it can be seen conditionally.