Facelets

Facelets is a JSF framework to implement UI templating (like tiles, sitemesh). You can use Tiles to implement the templating portion but Facelets is built for JSF.

In addition to the templating feature you can also create reusable components using Facelets and if you like Tapestry then you can make use of a similar feature with Facelets wherein rather than using jsf tags in the JSP you can use jsfc to indicate the component you plan to use. Example:

Note: I put this example together quite some time back but forgot to publish this earlier. Now straight to an example. I assume a certain knowledge of JSF as required. If not sure you can download the complete working zip file and get an idea for yourself regarding JSF and Facelets.

First of all you create the template.xhtml which will define the layout for our application:

The above illustrates a very basic example. In the zip file I do no use the above template, instead the zip file has more elaborate layout.

  • <ui:insert name=”title”> : Creates a placeholder to drop page titles.
  • <ui:insert name=”body”> : Creates a placeholder to drop page content.

Now here is my content page index..xhtml:

  • <ui:composition template=”/template.xhtml”> : The composition tag is used to identify the template to be used for this page.
  • <ui:define name=”title”> : The ui:define tag is used here to insert the page title
  • <ui:define name=”body”> : The ui:define tag is used here to insert the page contents.

The web.xml is:

Finally the faces-config.xml:

  • com.sun.facelets.FaceletViewHandler is the what does the templating magic.

Here is the screenshot of the home page:

aa

Clicking on Display All Notes will take you to the notes.xhtml page which is another static page with different content.

aaa

You can download the complete example by clicking here – faceletss.zip. Having done all of this I must say though that SiteMesh still remains my favorite templating engine. Not sure if it will work with JSF though.