282 CHAPTER 6: Component-Based Web Development Using JSF 2
Facelets
Facelets was created to replace the use of JSP as a view declaration language for JSF; it was
designed exclusively for JSF and provides templating and extensible tag libraries to refrain from
using scriptlets (Java code) in the HTML pages. The significant differences between Facelets and
JSP are that Facelets provides the ability to write pages in pure HTML markup and provides
server-side templating.
Templating with Facelets
You learned templating in Chapter 4 using the Tiles framework where you saw how templates
encapsulate common layout to be used across all the pages, and you now understand how
templates work. Facelets is similar to the Tiles framework for templating and composing pages.
Consequently, templating is very much the same, other than six tags provided by Facelets for
templating in the ui: tag library.
ui:composition
ui:decorate
ui:define
ui:include
ui:insert
ui:param
ui:composition
The ui:composition tag is used in template client files acting as template clients and indicates that
enclosing content should be included in the UIComponent hierarchy at the specific point in the page.
Listing 6-7 illustrates the syntax of ui:composition.
Note Contexts and dependency injection (CDI) beans are much more powerful than managed beans.
You should use CDI beans if you deploy your application in a Java EE application server such as Glassfish.
A Java EE 6 and newer application server automatically supports CDI. The CDI beans are used in the same
way as managed beans but instead of @ManagedBean, they are declared with the @Named annotation,
as shown here:
@Named("helloBean")
@SessionScoped
public class HelloBean implements Serializable {
...
}