Drawer DataMatrix in Java Decoration in JSF 12

Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.

As the preceding section shows, the decorator pattern greatly reduces the difficulty of providing a custom ViewHandler, but you still have to provide implementations for every abstract method on the ViewHandler class In Faces 12, an additional convenience to ease decoration is provided: the wrapper pattern This pattern obviates the need to laboriously override all the abstract methods of ViewHandler For example, consider the following

This little trick of object-oriented programming eliminates the need to manually override all the ViewHandler abstract methods

PART II

JSF 12 TIP The 12 version of the JSF specification also provides wrappers for StateManager

and ResponseWriter, in addition to ViewHandler The previous discussion covered the mechanics of building a custom ViewHandler The interesting part, of course, is in the details of what methods to override, and what to do in those methods These details are described in the chapter s later section titled The Design, Architecture, and Implementation of Facelets

General Considerations When Writing a Custom ViewHandler

Because ViewHandler is a singleton (a class that allows only one object instance per application), its methods are accessed by many threads simultaneously Therefore, the developer must take the usual precautions to ensure thread safety In particular, they should take great care not to store any per-request state as instance variables, and protect any initialization steps by either putting them in the constructor, known as eager initialization, or in a synchronized block that effectively executes only once per request, known as lazy initialization Before we jump into the specifics of Facelets, let s look at an example that illustrates a thread safety consideration In general, Facelets uses lazy initialization where possible to allow for faster startup times The following is an example of thread safety from the FaceletViewHandler

Let s examine the thread safety implications of this code To guard against the unlikely possibility of two different browsers accessing the site simultaneously before requests from any other browsers access the site, the checking and setting of the initialized instance variable is done inside a synchronized block

The Facelets View Description Technology

This section presents a brief but thorough guide to using Facelets view description technology, invented by JSF Expert Group member Jacob Hookom It also examines the implementation of Facelets to illustrate the details of creating a custom view description technology in Faces (Additional information on Facelets can be found at https://facelets devjavanet/)

The Power of Templating in Faces

The terms templating and composition are used interchangeably to describe building a single logical page view for the user out of several separate files Consider the following pseudo-code in a fictional markup language:

This shows the power of using a composition mechanism The constituent parts menubar, sidebar, and summary can be reused in multiple pages and their display can be customized by passing in parameters, optionally leveraging the EL Naturally, we expect that any Faces components contained within the included files will be added to the UIViewRoot and function as expected While it is possible to use JSP s jsp:include and <%@ include %>, and JSTL s c:import to do templating, these approaches have some drawbacks in the current version of the JSP and JSF specs For example, it is impossible to pass parameters from the host file to the included file The <jsp:param> feature doesn t work for JSF because it stores its content in page scope, which is not supported in JSF Furthermore, the current implementations of the containers can cause unexpected replication of the included content These drawbacks go beyond being mere bugs in a particular container s implementation Facelets addresses these problems by providing a simple, EL-aware, templating mechanism with full support for JSF s component model that supports parameter passing So let s recast the previous pseudo-code using Facelets

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 10 Transitional//EN" "http://wwww3org/TR/xhtml1/DTD/xhtml1-transitionaldtd"> <html xmlns="http://wwww3org/1999/xhtml" xmlns:ui="http://javasuncom/jsf/facelets" xmlns:h="http://javasuncom/jsf/html"> <!-- This is the main page for the application