jsf JSF Templates How to create a template


Setting up a template for one application

Create a file named template.xhtml under the /WEB-INF folder, that way the template files will be accessible only for the framework.


<!DOCTYPE html>
<html lang="en"

        <title><ui:define name="title">Default title</ui:define></title>

        <!-- Some styles that we might include for our whole application -->
        <h:outputStylesheet name="css/template.css" />

        <!-- Shared content for the application, e.g. a header, this can be an include -->
            Application Header

        <!-- The content we want to define in our template client -->
            <ui:insert name="content" />

This file will act as a template for the application. Now we'll define a specific view in our view directory.


<ui:composition template="/WEB-INF/template.xhtml"

    <ui:define name="title">Home</ui:define>

    <ui:define name="content">
        Welcome to the application!

Have a look at the template attribute in this client view, this tells JSF to use the template we want. Then, using <ui:define> we define specific content to be inserted where it is told in the template. Accessing /home.xhtml in client will render the whole result.