Facelets tags in JavaServer Faces
Facelets tags in JavaServer Faces (JSF) are essential components used for creating reusable templates, including defining content areas, including external content, and conditionally rendering content. These tags facilitate the separation of UI structure and content, enhancing code maintainability and reusability. Here’s an explanation followed by a Java example illustrating some commonly used Facelets tags:

Table of Contents
Explanation
- 1. Â <ui:composition> Tag:
- Purpose: Â Defines a Facelets composition template that can be used as a base layout for multiple pages.
- Usage: Â Typically used in template files (template.xhtml) to define the overall structure of a page.
- Example: Â <ui:composition template=”/WEB-INF/templates/basicTemplate.xhtml”>…</ui:composition>
- 2. Â <ui:include> Tag:
- Purpose: Â Includes the content of another Facelets file or JSF component into the current page.
- Usage: Â Useful for modular development where common content (like headers, footers) can be reused across multiple pages.
- Example: Â <ui:include src=”header.xhtml” />
- 3. Â <ui:define> and <ui:insert> Tags:
- Purpose: Â Used together to define sections within a template (<ui:define>) and insert content into those sections from child pages (<ui:insert>).
- Usage: Â Enables customization of specific areas (like content, header) within a template while maintaining a consistent layout.
- Example: Â
- xml
- <ui:define name=”content”>…</ui:define>
- <ui:insert name=”content”>Default Content</ui:insert>
- 4. Â <ui:param> Tag:
- Purpose: Â Passes parameters from a parent template to a child template or included page.
- Usage: Â Allows dynamic customization of included content based on parameters passed.
- Example: Â <ui:param name=”username” value=”{userBean.username}” />
- 5. Â <ui:remove> Tag:
- Purpose: Â Removes content conditionally based on a specified condition.
- Usage: Â Helps in conditional rendering of content within templates.
- Example: Â <ui:remove rendered=”{empty userBean.username}” />
Java Example
Here’s an example demonstrating the usage of some Facelets tags in JSF:
xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title><ui:insert name="title">Default Title</ui:insert></title>
</h:head>
<h:body>
<div id="header">
<ui:insert name="header">
<h1>Default Header</h1>
</ui:insert>
</div>
<div id="content">
<ui:insert name="content">
<p>Default Content</p>
</ui:insert>
</div>
<div id="footer">
<ui:insert name="footer">
<p>Default Footer</p>
</ui:insert>
</div>
</h:body>
</html>
xml
<ui:composition template="/WEB-INF/templates/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="title">Home Page</ui:define>
<ui:define name="header">
<h1>Welcome to My Website!</h1>
</ui:define>
<ui:define name="content">
<p>This is the home page content.</p>
</ui:define>
<ui:define name="footer">
<p>© 2024 My Website. All rights reserved.</p>
</ui:define>
</ui:composition>
Explanation of Example
- Facelet Template (template.xhtml):
- Defines a basic template structure using <ui:composition> with placeholders (<ui:insert>) for title, header, content, and footer.
- Child Page (home.xhtml):
- Uses <ui:composition> to inherit template.xhtml as the base layout.
- <ui:define> tags customize specific sections (title, header, content, footer) by inserting content into corresponding <ui:insert> tags defined in the template.
- Usage and Benefits:
- Developers create modular and reusable templates (template.xhtml) that define the overall layout and structure of pages.
- Child pages (home.xhtml) customize specific sections by providing content using <ui:define> tags, ensuring consistent UI across multiple pages.
Using Facelets tags in this manner enhances code maintainability, promotes reuse of UI components, and facilitates the creation of consistent and modular web applications in JSF. Understanding these tags is essential for effectively leveraging Facelets in Java web development.