Monday, December 22, 2014

Book Review: Mastering JavaServer Faces 2.2

Anghel Leonard's Mastering JavaServer Faces 2.2 (Packt Publishing, 2014) has the subtitle, "Master the art of implementing user interfaces with JSF 2.2." The book features twelve chapters and a short appendix spanning approximately 530 substantive pages. This blog post is my review of the PDF version of the book.

Preface

The Preface of Mastering JavaServer Faces 2.2 provides 2 to 3 sentences summarizing each of the book's chapters. It also recommends NetBeans 8+, GlassFish 4, and Mojarra JavaServer Faces 2.2.6 for running the book's sample applications. The "Who this book is for" section of the Preface clearly articulates that Mastering JavaServer Faces 2.2 is intended for readers who are already familiar with JavaServer Faces. This section states:

This book is a perfect symbiosis between JSF 2.0 and 2.2. It is dedicated to JSF developers who have previous experience and want to upgrade their knowledge to the new JSF 2.2. By fortifying your knowledge on JSF 2.0 and adding the power of JSF 2.2, you will soon become a JSF expert.

I re-emphasize this point here: Mastering JavaServer Faces 2.2 assumes JavaServer Faces experience. The book dives right into newer (JSF 2.2) JavaServer Features without the traditional background and introductory text one would expect from a book that introduces a language or framework. In other words, this book would be best suited for a developer with JSF experience who wants to learn about JSF 2 and JSF 2.2 new features or who has an older book on earlier versions of JSF and wants to use this book as a companion to that older book.

Chapter 1: Dynamic Access to JSF Application Data through Expression Language (EL 3.0)

The initial chapter of Mastering JavaServer Faces 2.2 begins fast with an introduction to Expression Language (EL). The chapter looks at EL 3.0 (JSR 341) syntax (operators, reserved words, immediate evaluation with ${}, deferred evaluation with #{}, referencing both CDI managed beans and JSF managed beans, referencing managed beans' properties and nested properties and methods, referencing Java enumerated types, accessing collections from JSF, and implicit objects).

I liked the Chapter 1 section on "writing a custom EL resolver" because I was not previously aware that one could write one's own custom implicit variables in JSF expression language. This section introduces EL Resolvers, demonstrates how to writ a custom EL Resolver, and describes how to differentiate between a VariableResolver and a PropertyResolver.

The "EL 3.0 overview" section of the first chapter introduces EL 3.0 (part of Java EE 7), lists some of its features, and describes how to use it with JSF. One of the more interesting portions of this section for me was the coverage of EL lambda expressions and streams.

Chapter 2: Communication in JSF

The second chapter of Mastering JavaServer Faces 2.2 opens with the statement, "Communication is the core of a JSF application, and is one of the main aspects that dictate the architecture of such an application." It adds, "JSF provides many solutions for ensuring a powerful and flexible communication layer between JSF components and also between JSF and XHTML pages, the JavaScript code, and other third-party components." With this introduction, the chapter moves onto covering a lengthy list of miscellaneous approaches to communicating between components of a JSF application.

The topics covered in Chapter 2 include context parameters, <f:param ...> tag and >c:set<, view parameters (UIViewParameter/<f:viewParam>), GET request view actions (<f:viewAction>), <f:attribute> tag (demonstrated with an example that modifies supported behavior of a PrimeFaces component), action listeners (<f:setPropertyActionListener>), JSF Flash scope, cookies, hidden fields (<h:inputHidden>), accepting passwords (<h:inputSecret>), accessing JSF user interface components from JSF Java code, EL method expressions, JSF data binding, injecting one managed bean into another managed bean (@ManagedProperty), and communication between managed beans.

Chapter 3: JSF Scopes – Lifespan and Use in Managed Beans Communication

Chapter 3 of Mastering JavaServer Faces 2.2 is all about scopes in JavaServer Faces. The chapter contrasts JSF managed beans and CDI beans and recommends "[using] CDI beans whenever possible," but does discuss situations in which JSF managed beans are preferable. This section includes a useful figure that makes it easy to see the JSF core scope annotations (javax.faces.bean) alongside the CDI scope annotations (javax.enterprise.context). The section describes each level of scope in a JSF application (request, session, view, application, conversation, flow, dependent pseudo-scope, none, custom) and whether JSF managed beans and/or CDI beans support each scope. This coverage of scopes includes code listings for each type of bean to which that scope applies. The sections on flow scope and custom scopoe are longer than the others and particularly useful in understanding these relatively more complicated scopes.

The third chapter concludes with brief sections on easy and lazy managed bean instantiation, allowable scopes for injected and injecting managed beans, and when to use @ManagedProperty versus @Inject. There are several useful figures in this chapter that use shapes and colors to illustrate points being made.

Chapter 4: JSF Configurations Using XML Files and Annotations – Part 1

Mastering JavaServer Faces 2.2's fourth chapter looks at how JSF 2.2 allows annotations to be used to configure many characteristics of a JSF application that formerly had to be configured in a faces-config.xml file. A table that is present early in this chapter maps old ("Before JSF 2.2") namespaces to new ("JSF 2.2") namespaces. The chapter also looks at specifying certain JSF characteristics both with XML and programmatically. The chapter has sections explaining how to use and order multiple configuration files and how to support internationalization by configuring locales and resource bundles.

Chapter 4 has a lengthy section on "configuring validators and converters." I like the author's discussion in this section regarding the state of injecting dependencies into validators and whether it's supported in JSF 2.2 or 2.3. Configuring JSF application navigation in another large topic in the fourth chapter that covers implicit navigation, conditional navigation, preemptive navigation (AKA predetermined navigation), and programmatic navigation.

Chapter 4's focus on configuration moves onto configuring action listeners, system event listeners, phase listeners, @ListenerFor and @ListenersFor.

Chapter 5: JSF Configurations Using XML Files and Annotations – Part 2

The fifth chapter of Mastering JavaServer Faces 2.2 continues the general theme of JSF configuration that the preceding chapter also addressed. The chapter begins with a relatively in-depth look at various approaches for configuring resource handlers (default configuration via placement in JSF 2.0 conventional locations, custom resource handler, and javax.faces.WEBAPP_RESOURCES_DIRECTORY JSF 2.0 context parameter in web.xml file). The section in Chapter 5 on configuring view handlers discusses and demonstrates writing custom view handlers to "convert absolute URLs into relative URLs" and to handle encountered ViewExpiredExceptions.

Chapter 5's section on overriding JSF renderers uses code listings and explanations to demonstrate overriding the HTML output by JSF components when rendered. I particularly liked the "little bit tricky" example of extending ResponseWriterWrapper to change the behavior of writeText to write escaped strings and making some other changes to support that PrimeFaces-like functionality in standard JSF. Another section in the chapter looks at extending ClientBehaviorBase and the other steps necessary to "define specific client-side behavior to a component in a reusable approach."

A considerable portion of Chapter 5 is devoted to JSF factories. Examples include configuring the global exception handler by extending ExceptionHandlerFactory and ExceptionHandlerWrapper, configuring the RenderKitFactory, configuring PartialViewContext, configuring VisitContext, configuring ExternalContext, and configuring Flash by extending FlashFactory and FlashWrapper. Each of these is explained by spelling out the steps needed to perform the configuration and then demonstrating those steps implemented with code listings.

Chapter 5 concludes with coverage of enabling and disabling the ClientWindow API, configuring the JSF lifecycle by extending LifecycleFactory and LifecycleWrapper, configuring an application by extending ApplicationFactory and ApplicationWrapper, and configuring View Declaration Language (VDL), and using more than one configured factory in conjunction with each other to write "a Java Servlet capable of converting a non-JSF request into a JSF view."

Chapter 6: Working with Tabular Data

Chapter 6 is devoted to coverage of applying <h:dataTable> to represent tabular data in JSF. Specific examples covered include rendering of tabular data from JSF-hosted POJOs, displaying a collection of objects with CollectionDataModel, presenting sorted data, an introduction to DataModel, deleting a row from a table, modifying a row in a table, adding a new row, supporting selection of a single row and multiple rows, nesting tables, paginating tables, filtering tables, and styling tables.

The sixth chapter also demonstrates how to generate a JSF table programatically with JSF API classes such as HtmlDataTable, HtmlColumn, and HtmlOutputText.

Chapter 7: JSF and AJAX

The seventh chapter of Mastering JavaServer Faces 2.2 covers use of JavaServer Faces with Ajax. The chapter introduces <f:ajax> and the JSF/Ajax lifecycle. It then provides detailed explanations of the main attributes of <f:ajax>: execute, render, listener, event, onevent, onerror.

Mastering JavaServer Faces 2.2's Chapter 7 covers more examples of using JSF with Ajax such as grouping multiple components within a single <f:ajax> tag, "updating input fields with Ajax after validation error" using resetValues, implementing Cancel and Clear buttons, mixing Ajax and Flow scope, determining if a request is an Ajax request (PartialViewContext.isAjaxRequest or headers Faces-Request and X-Requested-With), using <f:param> with <f:ajax>, controlling client-side Ajax queuing with <f:ajax>'s delay attribute, explicitly loading and customizing the JavaScript file that contains JSF's Ajax support (jsf.js).

Chapter 8: JSF 2.2 – HTML5 and Upload

Chapter 8 is introduced as being primarily composed of two sections: "The first part will present the JSF 2.2 support for HTML5, while the second part discusses the new upload component of JSF 2.2." The chapter's section on using HTML5 with JSF 2.2 discusses pass-through attributes and pass-through elements (TagDecorator). It also discusses application of Bean Validation 1.1 with HTML5 and JSF 2.2.

The section of Chapter 8 on JSF Upload introduces <h:inputFile> and some of its "most important" attributes before discussing use of multiple upload tags, identify characteristics of a file to be uploaded, writing uploaded data to disk, building a file upload validator, using upload with Ajax, uploading multiple files, upload with both indeterminate and determinate progress bars, and an especially lengthy discussion on providing previews of images to be uploaded.

Chapter 9: JSF State Management

The ninth chapter of Mastering JavaServer Faces 2.2 states early in the chapter that "JSF saves and restores the view state between requests using the ViewHandler/StateManager API. ... JSF uses this technique because it needs to preserve the views state over the HTTP protocol, which is a stateless protocol." The chapter goes on to explain how JSF 2.0 and 2.2 have improved JSF application performance with changes in how JSF state is managed. It provides a very detailed example of persisting state in JSF Mojarra to a MongoDB database.

One of the more interesting discussions in the ninth chapter for me was the discussion on JSF 2.2 support for stateless views via the transient attribute on the <f:view> element. Another interesting discussion covers security in JSF (CSRF, XSS, and SQL injection).

Chapter 10: JSF Custom Components

The opening statement of Chapter 10 is, "JSF is a component-based framework, and JSF custom components are the major proof that sustain JSF flexibility and extensibility." The chapter differentiates between custom components and composite components. I like the author's recommendation to look for already existing components as the first step when deciding to implement and then implement a custom component. This idea and a few other ideas are presented as possibly more appropriate approaches than implementing a custom component in many situations.

Chapter 10 explains and demonstrates "building noncomposite custom components" in JSF 2.2 with a single Java class annotated with @FacesComponent. The explanation and demonstration of writing noncomposite custom components dives into deeper details about implementing more complex custom components with Java classes and tag handlers.

Chapter 10's coverage of "building composite components" describes JSF 2 custom components as "practically custom components written in XHTML pages using markup tags" and is a longer and more detailed section than the section on noncomposite custom components. The author makes a good point here that use of XHTML "means that JSF page authors can start writing their components without having the same level of knowledge and skills as dedicated JSF component authors." This section demonstrates implementation of an example of a composite component that makes it easy to compare to the same example application implemented earlier in the chapter with a noncomposite custom component. Chapter 10 also provides explanations and illustrations of transforming the jQuery UI range slider component into a JSF composite component and transforming the HTML5 DatePicker into a JSF composite component.

There are several aspects of developing composite components in JSF covered in Chapter 10. These include working with composite facets, validating and converting input information within a composite component, distributing a composite component as a JAR, and instantiating composite components programatically.

Chapter 11: JSF 2.2 Resource Library Contracts – Themes

The second-to-last chapter of Mastering JavaServer Faces 2.2 starts with the sentence, "Starting with version 2.0, JSF developers exploit Facelets as the default View Declaration Language (VDL)." The early part of this chapter also describes a Facelet template as "a mix of XHTML and other resources such as CSS, JS, and images" that "acts as a base (or a model) for the application pages," providing "reusable code that serves as a consistent and standard look and feel." The chapter begins coverage of Resource Library Contracts by discussing the contracts folder. The chapter explains and illustrates applying style to tables and JSF components with contracts. The chapter also covers writing of contracts for composite components, writing custom themes, using XML to configure contracts, and packaging contracts in JARs under META-INF/contracts.

Chapter 12: Facelets Templating

Mastering JavaServer Faces 2.2 introduced and referenced Facelets in earlier chapters, but this final chapter focuses on Facelets (specifically templating with Facelets). After a brief history of how Facelets supplanted JavaServer Pages as the default View Definition Language for JavaServer Faces, the chapter provides a bullet list of Facelet tags with brief descriptions of each tag in each bullet.

After listing major Facelets tags with brief descriptions, Chapter 12 provides more in-depth discussion and examples of applying Facelets tags such as <ui:param>, <ui:decorate>, <ui:fragment>, <ui:repeat>, <ui:include>, <ui:debug>, and <ui:remove>.

Until reading Mastering JavaServer Faces 2.2, I was not aware of jsfc, but that is covered in the final chapter. The chapter also discusses "Facelets programmatic aspects" such as FaceletFactory, FaceletCache, ResourceHandler, including Facelets programatically, implementing a TagHandler class and implementing Facelets taglib functions.

The section of Chapter 12 titled "Facelets pitfalls" opens with something I've definitely observed: "It is a well-known fact that JSF pitfalls are not easy to understand and fix." This section then focuses on "three common Facelets pitfalls." Chapter 12's Summary is for that chapter only; there is no overall book summary.

Appendix: The JSF Life Cycle

This appendix is barely more than a single page, but has valuable information on the JSF lifecycle depicted in a flow diagram.

General Observations
  • There are numerous code listings in Mastering JavaServer Faces 2.2. These are black text on white background with no line numbers or syntax highlighting. Code is also available online, which is probably preferable because it can be presented in the reader's favorite IDE or text editor with color syntax highlighting and line numbers.
  • Mastering JavaServer Faces 2.2 provides graphics and figures to illustrate points being made. Many of these are in color in the PDF version I reviewed.
  • In multiple cases, the author mentioned a feature that was supposed to be part of JSF 2.x, but that was not working for him at time of writing of the book. I appreciated this being called out as it appears that some of these features may still not be supported in a standard way.
  • The author differentiates between standard JSF features and functionality and features and functionality provided by alternative JSF implementations.
  • NetBeans is referenced a few times in Mastering JavaServer Faces 2.2. In all of these cases, even a person who has not used NetBeans would likely understand the principle being stated.
  • Mastering JavaServer Faces 2.2 is not intended for the developer who is completely new to JavaServer Faces (the author stated this in the Preface: "It is dedicated to JSF developers who have previous experience and want to upgrade their knowledge to the new JSF 2.2."). Rather, this book is focused on providing in-depth details of new features of JSF 2.0 and 2.2 for developers who already have some basic familiarity with JSF. Alternative resources to consider for those entirely new to JSF in preparation for reading Mastering JavaServer Faces 2.2 include the following (some of which I've linked to earlier in this post):
Conclusion

Mastering JavaServer Faces 2.2 provides wide coverage of JavaServer Faces 2.0 and 2.2 additions that have made the framework more powerful and easier to apply. Mastering JavaServer Faces 2.2 assumes that the reader has previous JavaServer Faces experience and does not spend time bringing the reader up to speed on JSF basics before diving into the relatively detailed coverage of features new to JSF with 2.0 and 2.2. The book is best suited for developers who have mostly developed with JSF 1.x and want to start taking advantage of JSF 2 and JSF 2.2 features. This book is not appropriate for someone brand-new to JSF with no previous JSF experience.

No comments: