Technology

Component-Based Web Development Using JSF 2

Java Server Faces (JSF) is a component-based framework for developing web applications. The distinctive feature that sets component-based frameworks apart is the ability to create and distribute reusable UI components. In general, a component represents an abstraction, a well-defined contract with the implementation details hidden from the user of the component. That is, the user of the component does not need to know the internal mechanisms of the component in order to be able to use it. Struts and Spring Web MVC alleviate the increasing complexities of building sophisticated user interfaces for the Web, but these web frameworks are not component-centric and therefore unqualified for engineering truly reusable UI components.

To that end, different web frameworks, such as Tapestry and Wicket, emerged to offer a component-centric approach to web application development. However, because of a lack of existing standards for component-centric development, the ways in which these web frameworks implemented reusable UI components appeared to an experienced web developer to be tedious or limited.

Faces Servlet

Faces Servlet is the controller in the MVC and implements the Front Controller pattern that intercepts every interaction between Face let’s (the view) and the model. Faces Servlet is configured through annotations on managed beans, converters, components, renderers, and validators or optionally through the faces-config.xml descriptor file.

Managed Bean

Managed beans serve as the model for the UI component. They are responsible for the following:

  • Synchronizing data with components
  • Processing business logic
  • Handling navigation between pages

VDL

JSF uses a view declaration language (VDL) to display a page to the client on various devices such as the desktop, portables, and so on. The default VDL for Java Server Faces (JSF) is Face lets, but JSF allows multiple VDLs, such as JSP

JSF EL

In the Hello World application in Chapter 5, you saw how to access managed bean properties and invoke managed bean actions using EL expressions with the delimiters #{ and }. The EL used in JSF 1.0 and 1.1 (and later in JSP versions 1.2 and 2.0) was an extension of the EL that was part of the JSP Standard Tag Library (JSTL), as explained. The difference between JSF EL and JSP EL is that of evaluation. In JSP, as you have, any ${} expression that appears on the page is evaluated immediately during page rendering. Such expressions are called immediate expressions

Renderer

The renderer is responsible for displaying a component, in other words, rendering the markup to the client and translating a user’s input into the component’s value. JSF supports two programming models for displaying components.

n Direct renderer model: When the direct model is used, components decode from, and encode to, the view. The decoding and encoding processes are explained in the next section.

n Delegated renderer model: When the delegated model is used, the decoding and encoding are delegated to a renderer.

Converter and Validator

JSF provides out-of-the-box converters to convert its UI component’s data to objects used in a managed bean and vice versa. For example, they convert a component’s Date value to and from String values that come from the HTML markup. JSF also provides out-of-the-box validators to validate its UI components to ensure that the value entered by the user is valid. These tags can, for example, validate a range of Long or the length of a string.

Events and Event Listeners

When the user clicks a button or link on the JSF page, a JSF UI component triggers an event. To handle such an event, an event listener is registered on the managed bean. The UI component calls the event notification on the event listener for the specific event. As you have seen, JSF pages consist of a tree of components. This tree of components is managed by the JSF request processing life cycle behind the scenes. To understand the JSF request processing life cycle, first, you will create a Hello World web application, and then through this application, you will learn how the JSF life cycle works behind the scenes.

Summary

JSF is a component-based MVC framework, at the heart of which is the UI component model; this model allows the development of a web application’s view from collections of standard, out-of-the-box, reusable UI components. Unlike Struts and Spring Web MVC, the JSF life cycle performs mundane and recurring request-processing tasks in well-defined phases, allowing the developer to concentrate on the business logic of the web application.

Related Articles

Leave a Reply

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

Back to top button