JSF and Facelets


  • Facelets is a templating language developed, with JavaServer Faces (JSF) in mind, to address alot of the issues that have arisen when trying to use JSF with JavaServer Pages (JSP). These issues exist because JSP and JSF dont complement each other very well
  • Facelets provides – templating, code resue, easier development – it promotes a reduction in UI code


Originally, JSF was designed with the idea to reuse JSP as the main technology to create the page, since JSP already had a language that alot of the community was familiar with. In practise however, they are not so easy to use togethor.

JSPs were designed with the basic objective of creating a response to a request and to generate web content by processing a page from top to bottom. JSF has a more complex life cycle, treating the page as a collection of components, the rendering of which can happen in different phases. Where a JSP container generates output as soon as it encounters JSP content, JSF components dictate their own rendering and this causes problems.

Advantages of Facelets

Facelets replaces JSP with a simple API and is focused on buidling component trees and dealing with the complex JSF lifecycle. Some of the main reasons for using it are:

  • Facelets do not depend on a web container
  • They work with any version of JSF
  • JSTL cannot be used with JSF – Facelets provides a solution for this since it has Unified Expression Language (EL) support, including: support for EL functions, compile-time EL validation, deferred expression validation, JSTL iteration tags, method-invocation with expressions
  • Facelets have a faster compilation process than JSP since no Java bytecode is generated and compiled
  • Facelets provides templating so you can resuse your code to simplify development on large applications
  • Facelets has detailed error reporting

Creating an Application with Facelets

  1. Configure web.xml for facelt parameters

    – context parameter javax.faces.DEFAULT_SUFFIX – defines suffix of view documents, by convention this is *.xhtml

– other parameters can be optionally configured to configure facelet behaviour, eg:

facelets.BUFFER_SIZE – buffer size of response – by default is -1 (no buffer) – you could increase this to ensure that when an error occurs, the output is not sent to the client before the debug response is generated

facelets.DECORATORS – list of classes that implement TagDecorator
facelets.DEVELOPMENT – print debug info for errors
facelets.REFRESH_PERIOD – interval compiler checks for page changes – lower values useful during development (default is 2) – set to -1 if you dont want checks made

  1. Configuring the Faces Descriptor (faces-config.xml)

Facelets replaces the default JavaServer Faces ViewHandler with its own
implementation. To configure our web application to use the Facelets handler instead of the default JSF ViewHandler, we need to specify the <view-handler> element in the faces-config.xml file

  1. Create JSF Views using Facelets
    First we create the templates, which define the basic layout using the Facelets UI tags.
    we need to declare the facelet namespace and by convention define ui as the prefix for the Facelet tags – xmlns:ui=”http://java.sun.com/jsf/facelets

ui:insert – this tag defines an area that can be overwritten

ui:composition – references the template we want to use for the page. Everything inside this tag is evaluated by Facelets during compilation. Everything outside them is ignored

ui:define – defines the content that will go into the template placeholders. The name attribute must match that in the relevant ui:insert tag in the template

ui:include – allows us to include the content from another document

<h:commandLink action=”home” /> – this creates a link element with an action of ‘home’. We define the navigation rules in the facelet configuration, eg. <navigation-rule>

Leave a Reply

Your email address will not be published. Required fields are marked *