How to create a reuseable template with header/footer/navigation?

14,596

This sounds like a classic case of a master template. In such a template you put everything that's common to all pages and then your actual pages reference this template and "fill in the blanks". In a way it's the reverse of the also classic include.

E.g.

/WEB-INF/templates/masterTemplate.xhtml:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
>
    <h:head>
        <title>
            <ui:insert name="title">Some title</ui:insert>
        </title>        
    </h:head>

    <ui:include src="header.xhtml"/>

    <h:body>
        <ui:insert name="content" />
    </h:body>

    <ui:include src="footer.xhtml"/>

</html>

A page uses this as follows, e.g.

/hello.xhtml

<ui:composition template="/WEB-INF/templates/masterTemplate.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
>
   <ui:define name="title">hello</ui:define>

    <ui:define name="content">
        Hi, this is the page
    </ui:define>
</ui:composition>
Share:
14,596
user1111928
Author by

user1111928

Updated on June 04, 2022

Comments

  • user1111928
    user1111928 almost 2 years

    I have been playing with JSF and have a project working that has a header/footer/navigation/content panels. The project, however, goes from page 1 to page 2, etc., with each page having a different layout. How can I create a reusable template that keeps the same look and feel from page to page, i.e., header/footer/navigation stay the same, but content is updated?