The Java EE 6 Tutorial, Volume I

The Facelets Template and Page

To use the managed bean in a simple Facelets application, you can use a very simple template file and index.xhtml page.

The template page, template.xhtml, looks like this:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="resources/css/default.css" rel="stylesheet" type="text/css"/>
        <title><ui:insert name="title">Default Title</ui:insert></title>

        <div id="container">
            <div id="header">
                <h2><ui:insert name="head">Head</ui:insert></h2>

            <div id="space">

            <div id="content">
                <ui:insert name="content"/>

To create the Facelets page, you can redefine the title and head, then add a small form to the content:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""
    <ui:composition template="/template.xhtml">

        <ui:define name="title">Simple Greeting</ui:define>
        <ui:define name="head">Simple Greeting</ui:define>
        <ui:define name="content">
            <h:form id="greetme">
               <p><h:outputLabel value="Enter your name: " for="name"/>
                  <h:inputText id="name" value="#{}"/></p>
               <p><h:commandButton value="Say Hello" action="#{printer.createSalutation}"/></p>
               <p><h:outputText value="#{printer.salutation}"/> </p>


The form asks the user to type a name. The button is labeled “Say Hello,” and the action defined for it is to call the createSalutation method of the Printer managed bean. This method in turn calls the greet method of the defined Greeting class.

The output text for the form is the value of the greeting returned by the setter method. Depending on whether the default or the @Informal version of the greeting is injected, this is either

Hello, name.


Hi, name!

where name is the name typed by the user.

The Facelets page and template are located in the following directory:


The simple CSS file that is used by the Facelets page is in the following location: