Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
When we heard about JavaServer Faces (JSF) at the 2002 JavaOne conference, we were very excited. Both of us had extensive experience with client-side Java programming—David in Graphic Java™, and Cay in Core Java™, both published by Sun Microsystems Press—and we found web programming with servlets and JavaServer Pages (JSP) to be rather unintuitive and tedious. JSF promised to put a friendly face in front of a web application, allowing programmers to think about text fields and menus instead of dealing with page flips and request parameters. Each of us proposed a book project to our publisher, who promptly suggested that we should jointly write the Sun Microsystems Press book on JSF.
In 2004, the JSF Expert Group (of which David is a member) released the JSF 1.0 specification and reference implementation. A bug fix 1.1 release emerged shortly afterward, and an incremental 1.2 release added a number of cleanups and convenience features in 2006.
The original JSF specification was far from ideal. It was excessively general, providing for use cases that turned out to be uninteresting in practice. Not enough attention was given to API design, forcing programmers to write complex and tedious code. Support for GET requests was clumsy. Error handling was plainly unsatisfactory, and developers cursed the “stack trace from hell”.
JSF had one saving grace, however. It was highly extensible, and therefore it was very attractive to framework developers. Those framework developers built cutting edge open-source software that plugged into JSF, such as Facelets, Ajax4jsf, Seam, JSF Templates, Pretty Faces, RichFaces, ICEFaces, and so on.
JSF 2.0, released in 2009, is built on the experience of those open-source frameworks. Nearly all of the original authors of the aforementioned frameworks participated on the JSF 2 Expert Group, so JSF 2.0, unlike JSF 1.0, was forged from the crucible of real-world open-source projects that had time to mature.
JSF 2.0 is much simpler to use and better integrated into the Java EE technology stack than JSF 1.0. Almost every inch of JSF 1.0 has been transformed in JSF 2.0 in some way for the better. In addition, the specification now supports new web technologies such as Ajax and REST.
JSF is now the preeminent server-side Java web framework, and it has fulfilled most of its promises. You really can design web user interfaces by putting components on a form and linking them to Java objects, without having to mix code and markup. A strong point of JSF is its extensible component model, and a large number of third-party components have become available. The flexible design of the framework has allowed it to grow well and accommodate new technologies.
Because JSF is a specification and not a product, you are not at the mercy of a single vendor. JSF implementations, components, and tools are available from multiple sources. We are very excited about JSF 2.0, and we hope you will share in this excitement when you learn how this technology makes you a more effective web application developer.
This book is suitable for web developers whose main focus is on implementing user interfaces and business logic. This is in stark contrast to the official JSF specification, a dense and pompously worded document whose principal audience is framework implementors, as well as long-suffering book authors. JSF is built on top of servlets, but from the point of view of the JSF developer, this technology merely forms the low-level plumbing. While it can’t hurt to be familiar with servlets, JSP, or Struts, we do not assume any such knowledge.
The first half of the book, extending through Chapter 7, focuses on the JSF tags. These tags are similar to HTML form tags. They are the basic building blocks for JSF user interfaces. Anyone with basic HTML skills (for web page design) and standard Java programming (for the application logic) can use the JSF tags to build web applications.
The first part of the book covers these topics:
Setting up your programming environment (Chapter 1)
Connecting JSF tags to application logic (Chapter 2)
Navigating between pages (Chapter 3)
Using the standard JSF tags (Chapter 4)
Using Facelets tags for templating (Chapter 5)
Data tables (Chapter 6)
Converting and validating input (Chapter 7)
Starting with Chapter 8, we begin JSF programming in earnest. You will learn how to perform advanced tasks, and how to extend the JSF framework. Here are the main topics of the second part:
Event handling (Chapter 8)
Building composite components—reusable components with sophisticated behavior that are composed from simpler components (Chapter 9)
Ajax (Chapter 10)
Implementing custom components (Chapter 11)
Connecting to databases and other external services (Chapter 12)
We end the book with a chapter that aims to answer common questions of the form “How do I . . . ?” (Chapter 13). We encourage you to have a peek at that chapter as soon as you become comfortable with the basics of JSF. There are helpful notes on debugging and logging, and we also give you implementation details and working code for features that are missing from JSF, such as file uploads, pop-up menus, and a pager component for long tables.
All chapters have been revised extensively in this edition to stress the new and improved features of JSF 2.0. Chapters 5, 9, and 10 are new to this edition.
All software that you need for this book is freely available. You can use an application server that supports Java EE 6 (such as GlassFish version 3) or a servlet runner (such as Tomcat 6) together with a JSF implementation. The software runs on Linux, Mac OS X, Solaris, and Windows. Both Eclipse and NetBeans have extensive support for JSF development with GlassFish or Tomcat.
The web site for this book is http://corejsf.com. It contains:
The source code for all examples in this book
Useful reference material that we felt is more effective in browseable form than in print
A list of known errors in the book and the code
A form for submitting corrections and suggestions