Sorry, what is Facelets?

Lets see!

Start with JSP please..

JSP's main mission in life is to generate a response to a request; a JSP page is processed in one pass from top to bottom, with JSP action elements processed in the order in which they appear in the page.

JSP is a templating language that produces a Servlet. The body of the JSP becomes the equivalent of a servlet's doGet() and doPost() methods.

Moving on to Facelets..?

Facelets is a templating language developed from the ground up with JavaServer Faces in mind. Main concern of Facelets is creating a tree of UIComponent objects and interweaving content completely within the JSF request processing lifecycle.

Top properties of JSF are:
        • Templating
        • Code Reuse
        • Ease of Development

When JavaServer Faces (JSF) was devised, the intention was to reuse JSP as the main technology to create pages, as it was already a standard in the web community.

What is the wrong with JSF + JSP?

Unfortunately, JSP and JSF don’t naturally complement each other. JSP is used to create static or dynamic web content but not to create component trees. Its elements are processed in a page from top to bottom with the basic objective of creating a response to a request. JSF, however, has a more complex life cycle, and component generation and rendering happen in clearly separated phases.

When used together, JSP and JSF both write output to the response, but they do so differently: the JSP container creates output as soon as it finds JSP content, whereas JSF components dictate its own rendering. This difference can cause some problems.

    <h:outputText value="I am first!" />
    I am second!

 The output will be:
I am second!
I am first!

The JSP container adds the "I am second" text as soon as it encounters it, whereas the h:panelGroup, which is a component that renders its own children, won’t produce the "I am first" output until the closing tag is reached.

And Facelets solves this problem?

Facelets fills the gap between JSP and JSF. It is a view technology focused on building component trees and interweaving content with the complex JSF life cycle.

Facelets operates completely within the JSF request processing lifecycle and defers rendering components until the render response phase.  The framework is focused on one specific task and that’s tree creation and content interweaving—taking an XHTML or XML document and using it as a set of instructions to populate a tree of UIComponents.

What does a Facelets file look like?

<html xmlns=""
      <!-- body content goes here -->
      Hello #{}!

Facelets and JSF standard components?

Facelets comes ready to use all of the UIComponents in the JavaServer Faces API. There is a UI tag library built into Facelets which gives you all of the templating and re-use capabilities.

Facelets and JSF lifecycle?

Java ServerFaces performs tasks in a certain order ( The FaceletViewHandler is invoked in just two phases of the Faces lifecycle: the Restore View and Render Response phases.

In the Restore View phase, the ViewHandler makes a view available for processing in subsequent phases. Here the FacesServlet searches the FacesContext for the request. If the FacesContext contains a UIRootView, the FacesServlet calls the restoreView method of the ViewHandler, which restores the state of the view, stores the UIRootView in the FacesContext, and sets the value of those components that have value bindings. If the FacesContext does not contain an existing UIRootView, the FacesServlet calls the createView method of the ViewHandler, which creates a new view, and stores the UIRootView in the FacesContext. At the end of this phase, the FacesContext contains a UIRootView, which is either a restored previous view or a new view.

In the Render Response phase, the FacesServlet calls the renderView method of the ViewHandler class, which renders the response to the client for the currently selected view and saves the state of the response for processing in subsequent requests.

Facelets uses one or more XHMTL documents as a set of instructions to populate a tree of UIComponent objects. Most of the custom logic of the FaceletViewHandler is executed in the restoreView and renderView methods to build a tree of UIComponent objects.

Any last words?

JSP was developed to integrate static and dynamic content within a simple request and response lifecycle. Facelets was designed to integrate into the JSF component model and more complicated six-phase lifecycle.