Standard Syntax:
     <%@ taglib prefix="ui" uri="http://java.sun.com/jsf/facelets" %>

XML Syntax:
     <anyxmlelement xmlns:ui="http://java.sun.com/jsf/facelets" />


The tags in this library add templating—a powerful view composition technique—to JSF. Templating is so useful that there are entire frameworks, such as Tiles and SiteMesh, that are built around the concept of templating. So what is templating, how can you benefit from it, and how does this tag library implement it?

If you've used JSP before, you've probably used jsp:include. The prototypical example for jsp:include is a header on each page in a web application. One JSP page, say header.jsp, encapsulates the header content, and the header is included by each page. You encapsulate and reuse content, so that changes to one file, header.jsp, affect the header on every page.

This tab library contains a tag—ui:include— that's analagous to jsp:include, but encapsulating and reusing content is only half the templating story, because templating also lets you encapsulate and reuse layout. You define a single template (meaning layout), and you reuse that template with multiple compositions. So now you can control the layout of many pages with a single template (layout). Let's take a look at an example.

A Templating Example

First, we define a template:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5.             xmlns:ui="http://java.sun.com/jsf/facelets"
  6.     <head>
  7.       <link href="styles.css" rel="stylesheet" type="text/css"/>
  8.       <title><ui:insert name="title">Default Title</ui:insert></title>
  9.     </head>
  11.     <body>
  12.       <ui:debug/>
  13.       <div class="heading">
  14.         <ui:insert name="heading"/>
  15.       </div>
  17.       <div class="content">
  18.         <ui:insert name="content"/>
  19.       </div>
  20.     </body>
  21. </html>

In the preceeding listing, we've defined a layout, also known as a template. That template uses the ui:insert tag to insert pieces of a page —namely, title, heading, and content— defined in a composition. Notice that on line 8, we define a default title, in case one isn't provided by the composition. Also note that on line 12 we have the ui:debug tag, which lets the user activate a popup window with debugging information by typing CTRL + Shift + d.

The title, heading, and content pieces of the page referenced in the template are defined in a separate XHTML file in a composition, like this:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5.    xmlns:ui="http://java.sun.com/jsf/facelets">
  7.   <body>
  8.     <ui:composition template="/layout.xhtml">
  10.       <ui:define name="title">A List of Contacts</ui:define>
  11.       <ui:define name="heading">Contacts</ui:define>
  13.       <ui:define name="content">
  14.         <ui:include src="contactsTable.xhtml" />
  15.       </ui:define>
  17.     </ui:composition>
  18.   </body>
  19. </html>

At runtime, JSF synthesizes the two previous XHTML pages to create a single JSF view by inserting the pieces defined in the composition into the template (that template is layout.xhtml, which is the first listing above). JSF also disregards everything outside of the composition tag so that we don't wind up with two body elements in the view. Also, note that we use the ui:include tag on line 14 to include content (which happens to be a table) from another XHTML page, so that we can reuse that table in other views.

So why do we have two XHTML pages to define a single view? Why not simply take the pieces and manually insert them into the layout so that we have only a single XHTML page? The answer is simple: we have separated layout from the content so that we can reuse that layout among multiple compositions. For example, now we can define another composition that uses the same layout:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5.       xmlns:ui="http://java.sun.com/jsf/facelets">
  7.   <body>
  8.     <ui:composition template="/layout.xhtml">
  10.       <ui:define name="title">Create a Contact</ui:define>
  11.       <ui:define name="heading">Create Contact</ui:define>
  13.       <ui:define name="content">
  14.         <ui:include src="createContactForm.xhtml"/>
  15.       </ui:define>
  17.     </ui:composition>
  18.   </body>
  19. </html>

By encapsulating the layout, we can reuse that layout among multiple compositions. Just like ui:include lets us encapsulate and reuse conent, JSF compositions let us encapsulate and reuse layout, so that changes to a single layout can affect multiple views. Fundamentally, that's what this tag library is all about.

Tag Library Information
Display NameNone
Short Nameui

Tag Summary

This tag is the same as the ui:composition, except for two things: JSF creates a component and adds it directly to the tree, and there's no associated template.

Use this tag to create a component and specify a filename for the component as either the source of a ui:include, or the source of a Facelets tag.


Defines a composition that optionally uses a template, as outlined in the description of the ui tag library. Multiple compositions can use the same template, thus encapsulating and reusing layout. JSF disregards everything outside of the composition, which lets developers embed compositions in well-formed XHTML pages that can be viewed in an XHTML viewer, such as Dreamweaver or a browser, without including extraneous elements such as head and body.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5.    xmlns:ui="http://java.sun.com/jsf/facelets">
  7.   <body>
  10.     <ui:composition template="/layout.xhtml">
  12.       <ui:define name="title">#{msgs.contactsWindowTitle}</ui:define>
  13.       <ui:define name="heading">#{msgs.contactsHeading}</ui:define>
  15.       <ui:define name="content">
  16.         <ui:include src="contactsTable.xhtml" />
  17.       </ui:define>
  19.     </ui:composition>
  22.   </body>
  23. </html>

When the ui:debug tag is placed in an XHTML page, it creates a component and adds it to the component tree. That debug component captures debugging information, namely the current state of the component tree and the scoped variables in the application, when the component is rendered. If the user presses CTRL + SHIFT + d, JSF opens a window that shows the debugging information captured by the debug component.

Typically, the best place to put the ui:debug tag is in an application's main template, which lets developers enable or disable viewing of debugging information in one central location. Additionally, page authors can change the hotkey (which by default is CTRL + SHIFT + d, where the d stands for debug) to CTRL + SHIFT + ?, where ? represents the key specified as the value of the hotkey attribute.

You can use the rendered attribute to control whether the debug component is rendered. Using an EL expression as the value for the rendered attribute lets you control whether debug output is enabled for multiple views based on a single bean property.


The define tag defines content that is inserted into a page by a template. The define tag can be used inside ui:composition, ui:component, ui:decorate, and ui:fragment tags.

Content defined by the define tag can be inserted into a page by using ui:insert.


The decorate tag is identical to the composition tag, except that ui:decorate, unlike ui:composition, does not disregard all content outside of the tag. The decorate is useful when you want to decorate some content in a page, for example, you might want to decorate a list of items, like this:

  1. <ui:decorate template="/layout.xhtml">
  2.   <ui:define name="listHeading">
  3.     <ui:include src="shared/listHeading.xhtml"/>
  4.   </ui:define>
  6.   <c:forEach items="#{items}" var="item">
  7.     ...
  8.   </c:forEach>
  9.   ...
  10. </ui:decorate>

Because JSF does not disregard everything outside of the ui:decorate tag, ui:decorate can be used to decorate pieces of a page.

The fragment tag is identical to the component tag, except that ui:fragment, unlike ui:component, JSF does not disregard all content outside of the tag.


Use this tag—which is very similar to JSP's jsp:include—to encapsulate and reuse content among multiple XHTML pages. There are three things this tag can include: plain XHTML, and XHTML pages that have either a composition tag or a component tag.

You supply a filename, through ui:include's src attribute for JSF to include. That filename is relative to the XHTML file that was rendered as a result of the last request. So, for example, if JSF loaded the view login.xhtml, and that file included pageDecorations/header.xhtml, and pageDecorations/header.xhtml included companyLogo.xhtml, then companyLogo.xhtml will not be found if it's in the pageDecorations directory, because companyLogo.xhtml has to be in the same directory as login.xhtml.


Inserts content into a template. That content is defined—with the ui:define tag— in either a ui:composition, ui:component, ui:decorate, or ui:fragment.


Use this tag to pass parameters to an included file (using ui:include), or a template (linked to either a composition or decorator). Embed ui:param tags in either ui:include, ui:composition, or ui:decorate to pass the parameters.


Use this tag as an alternative to h:dataTable or c:forEach, especially when you are using the jsfc feature of Facelets. You can specify this component as the value of the jsfc attribute, like this: <div... jsfc="ui:repeat" value="#{contacts}" var="contact">...


Remove content from a page. This tag is often used in conjunction with the jsfc feature of Facelets, to wrap additional markup. When Facelets removes markup from a page by substituting markup items that have a jsfc attribute with the specified component, Facelets also removes anything in the page that is contained in a ui:remove tag.


Java, JSP, and JavaServer Pages are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright 2002-3 Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA 95054, U.S.A. All Rights Reserved.