JSF Central - Inside Facelets Part 1: An Introduction
JSF Central

 
 Home 
 
 Products 
 
 Articles & Books 
 
 Resources 
Inside Facelets
 
Inside Facelets Part 1: An Introduction
by Jacob Hookom
17 Aug 2005 01:00 EDT

This is the first of a series of articles about Facelets, an alternative view technology for building JSF applications. Facelets is a powerful templating system that allows you to define JSF views using HTML-style templates, reduces the amount of code necessary to integrate components into the view, and doesn't require a web container. In this article, Jacob introduces Facelets, explaining some of its features, and explaining why he started the project. Part 2 of the series shows you how to use Facelets.


The web community is eagerly seeking a framework like Tapestry, backed by JavaServer Faces as the industry standard. While JavaServer Faces and JSP are meant to be aligned, Facelets steps outside of the JSP specification and provides a highly performant, JSF-centric view technology. Anyone who has created a JSP page will be able to do the same with Facelets and familiar XML-tag use. The difference is under the hood where all the burden of the JSP vendor API is removed to greatly enhance JSF as a platform and provide easy plug-and-go development without requiring JSP tag development.

JavaServer Faces UIComponents are first class citizens within Facelets; there's no need to develop extra objects to integrate. There's also no need to learn yet another XML schema to define your views.

Here's an example of how easy it is to define a view with Facelets:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hello</title>
</head>
<body>
	<form id="helloForm" jsfc="h:form">
		<h2>
			Hi. My name is Duke.  I'm thinking of a number from
			#{UserNumberBean.minimum} to #{UserNumberBean.maximum}.
			Can you guess it?
		</h2>
		<img id="waveImage" src="wave.med.gif" />
		
		<input type="text" jsfc="h:inputText" id="userNo"
		       value="#{UserNumberBean.userNumber}"
			   validator="#{UserNumberBean.validate}"/>
			   
		<input type="submit" jsfc="h:commandButton" id="submit"
		       action="success" value="Submit" />
		<p />
		<h:message showSummary="true" id="errors1" for="userNo"/>
		<p />
		<c:forEach begin="1" end="4" varStatus="v">
		#{view.viewId} #{v.index}<br/>
		</c:forEach>
	</form>

</body>
</html>

You may be thinking that the markup looks extremely familiar. That's the point. There's no need to learn another templating language or schema.

Why Use Facelets?

First and foremost, Facelets isn't dependent on a JSP container. This means that you can start using the new JSF 1.2 features without waiting for a JSP 2.1 container. There were some serious issues with combining JSF 1.1 and JSP use when it first came out. Many of these issues were highlighted in an article by Hans Bergsten titled, "Improving JSF by Dumping JSP." Reading that article is strongly recommended as a case for moving to JSF 1.2 and Facelets.

Secondly, Facelets is designer friendly. Taking from Tapestry's jwcid attribute, Facelets uses jsfc and proper namespaces to convert HTML elements to their associated JSF component. In the example above, jsfc (JSF Compilation) is used to tell the compiler to instead build a h:inputText component into the view.

Last to be mentioned here, Facelets is very light weight. Some of you may be familiar with Velocity's capabilities as a templating engine. Facelets has the same capabilities such that you can define and test JSF views without a Servlet container. The framework overhead is minimal and practical for production use. It also opens the doors for JSF use in deployments outside of servlets, including Portlets.

Here's how you create a new Facelet:

// grab our FaceletFactory and create a Facelet
FaceletFactory factory = FaceletFactory.getInstance();
Facelet f = factory.getFacelet(viewToRender.getViewId());

// populate UIViewRoot
f.apply(context, viewToRender);

Short List of Features

Here's a short list of Faclet's current feature set:
  • Works with JSF 1.1 and JSF 1.2, including Sun's RI and Apache MyFaces.
  • Zero Tag development time for UIComponents
  • Fast Templating/Decorators for Components and Pages
  • The ability to specify UIComponent trees in separate files (UICompositions)
  • Line/Tag/Attribute precise Error Reporting
  • Specify Tags in Separate Files, even packaged with Jars
  • Full EL support, including Functions
  • Compile-time EL Validation
  • XML configuration files aren't necessary, but are available
  • Reserves the 'jsfc' attribute which acts the same as Tapestry's jwcid (Example: <input id="bar" type="text" jsfc="h:inputText" value="#{foo.bar}"/>)
  • Plugable Decorators to really make designer's job easy (Example: transform <input type="text"/> to <h:inputText/> at compile time)
  • Does *not* required any special RenderKits
  • Facelets isn't dependent on a Web Container

Why Facelets will Succeed

Everyone wants to be more designer friendly, and Tapestry seems to be the only choice developers are pursuing. On the other hand, JSF is the standard everyone would like to have happen, but JSF needs a more "pluggable" ViewHandler framework that is both designer and developer friendly.

Developers go through enough work defining UIComponents/Converters/Validators in their faces-config.xml, and Facelets only asks them to specify a name alias to plug their objects into their pages (no XML necessary). The key is ease of integration and development.

In conclusion, Facelets is a clean slate for correcting concerns with JSF. Templating, re-use, and ease of development are top priorities that will help bring developers on board with JSF as a suitable platform for large scale projects.



RSS feed(all feeds)

The Editor's Desk
Podcasts
Inside Facelets
In the Trenches

Site version 1.83  Report web site problems

Copyright (C) 2003-2014 Virtua, Inc. All Rights Reserved. Java, JavaServer Faces, and all Java-based marks are trademarks or registered trademarks of Oracle Corporation. in the United States and other countries. Virtua, Inc. is independent of Oracle Corporation. All other trademarks are the sole property of their respective owners.