JavaServer Faces (JSF) simplifies the development of web application user interfaces, allowing Java programmers to develop the application backend without worrying about a lot of details. This article offers a brief introduction to the technology. It is excerpted from chapter one of the book JavaServer Faces, written by Hans Bergsten (O'Reilly, 2004; ISBN: 0596005393).
Over the last few years, Java has established itself as the leading technology for web application development. Developers are using technologies like servlets and JSP to develop scalable and robust browser-based user interfaces for countless applications with great success. But as web applications become more complex, some developers are longing for the good ol' days of traditional graphical user interface (GUI) frameworks with rich, powerful user interface widgets and event-driven development models. Servlets and JSP have served us well, but HTTPs stateless nature and simple, coarse-grained request/response model forces application developers using these technologies to struggle with details that are handled behind the scenes by GUI frameworks like AWT/Swing, the standard GUI framework for Java.
To make it easier to develop sophisticated web application user interfaces, open source projects and commercial companies have developed frameworks that mimic traditional GUI frameworks as far as possible. Some notable examples are Enhydra's Barracuda, Apache's Tapestry, Oracle's UIX, and Sun's JATO. In the spring of 2001, a Java Community Process (JCP) group was formed with representatives from most of these efforts (including yours truly) to come up with a standard solution that all frameworks can use. The result is JavaServer Faces; the 1.0 version of the specification was released in March 2004.
What Is JavaServer Faces?
JavaServer Faces (JSF) simplifies development of sophisticated web application user interfaces, primarily by defining a user interface component model tied to a well-defined request processing lifecycle. This allows:
Java programmers to develop the application backend without worrying about HTTP details and integrate it with the user interface through a familiar event-driven model with type-safe interfaces.
Page Authors without programming knowledge to work with the user interface "look and feel" aspects by assembling components that encapsulate all user interaction logic-- thereby minimizing the need for program logic embedded directly in the user interface pages.
Vendors to develop powerful tools for both frontend and backend development.
More specifically, JSF is a specification with implementations offered by multiple vendors. It defines a set of user interface (UI) components--basically, a one-to-one mapping to the HTML form element set plus a few extras--that can be used right out of the box, as well as an Application Programming Interface (API) for extending the standard components or developing brand new components. Validators attached to the components validate user input, which is then automatically propagated to application objects. Event handlers are triggered by user actions, such as clicking on a button or a link, and can change the state of other components or invoke backend application code. The outcome of the event processing controls which page is displayed next, with help from a pluggable navigation handler.
While HTML is the markup language of choice for most web applications today-- and used for most examples in this book--JSF is not limited to HTML or any other markup language. Renderers that are separate from the UI components control the actual markup sent to the client, so the same UI component coupled with different renderers can produce very different output--for instance, either HTML and WML elements. If you're familiar with Swing, think "pluggable look and feel" (PLAF).
JSF gives you lots of flexibility in how you actually develop the user interface. All JSF implementations are required to support JavaServer Pages (JSP) as a presentation layer technology, with JSF components represented by JSP custom action elements (also commonly known as custom tags). The JSF API, however, is flexible enough to support other presentation technologies besides JSP. For instance, you can use pure Java code to create JSF components, which is similar to how a Swing UI is developed. Alternatively, you can bind JSF components to nodes in templates described by plain HTML files, the approach explored by projects such as Barracuda/XMLC and Tapestry. I use the JSP layer for most examples in this book because it's the only approach thats completely specified in JSF 1.0, but I also show examples of other approaches.
If you've developed Java web applications for some time, you've probably heard of (and have likely used) application frameworks like Apache Struts (http://jakarta.pache.org/struts/) or Maverick (http://mav.sourceforge.net/). Even though JSF overlaps a bit with the features offered by frameworks like these (for instance in the areas of validation and navigation support), it's also designed to play well with them. I discuss the options available for Struts applications to use a JSF user interface in Chapter 12, so you'll see exactly how JSF fits together with this popular application framework.