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).
Introducing JavaServer Faces - More Modular User Interface Code (Page 3 of 5 )
The web application architecture most experienced developers recommend is loosely based on the Model-View-Controller (MVC) design pattern. MVC was first described by Xerox in a number of papers published in the late 1980s, in conjunction with the Smalltalk language. This model has since been used for GUI applications developed in all popular programming languages. The basic idea is to separate the application data and business logic, the presentation of the data, and the interaction with the data into distinct entities labeled the Model, the View, and the Controller, respectively.
The Model represents pure business data and the rules for how to use this data; it knows nothing about how the data is displayed or the user interface controls used to modify the data. The View, on the other hand, knows all about the user interface details. It also knows about the public Model interface for reading its data, so that it can render it correctly, and it knows about the Controller interface, so it can ask the Controller to modify the Model. Using the MVC design pattern results in a flexible application in which multiple presentations (Views) can be provided and easily modified, and changes in the business rules or physical representation of the data (the Model) can be made without touching any of the user interface code.
While Java web application frameworks like Struts support the MVC model on a high level, its not supported in the same strict, fine-granular way as in a GUI framework. For example, in Struts, the View is represented by a set of JSP pages, the Controller by a Struts servlet that delegates the real work to Action classes, and the Model by application classes that typically interact with a database or some other permanent storage. The interfaces between the different pieces, however, are not defined as a set of specific methods with declared parameters. Instead, control is passed between the pieces using generic methods, and the data the next piece needs is made available through coarse-grained collections--such as collections of all HTTP request parameters, headers, attributes, and so on. Another major difference between a GUI framework and a typical web application framework is that the latter only recognizes one type of event: the HTTP request. The code that handles this coarse-grained event has to dig through the request data to tell if the user has entered new data in any fields, asked for a different type of presentation by selecting an option in a menu, ordered a transaction to be completed, or something else. A GUI framework, on the other hand, is based on fine-granular events, such as "value changed," " menu item selected," and "button clicked," with separate event handlers dealing with each event.
The JSF MVC design is similar to Swing and other GUI frameworks, as shown in Figure 1-2.
Figure 1-2.JTheJSF MVC design
The Model is represented by properties of application objects, e.g., ausernameproperty on an application object holding user information. JSF UI components declare what events they can fire, such as "value changed" and "button clicked" events, and external event listeners (representing the Controller) attached to the components handle these events. An event listener may set properties of the JSF components--for instance, adjust the set of rows shown in a table--or invoke backend code that processes the submitted data (say, verify that a credit card is valid or update a database). A separate renderer class (representing the View) renders each JSF UI component, making it possible to render instances of the same component type in different ways (e.g., either as a button or a link, or using different markup languages) just by attaching different renderers.
Besides the UI components, JSF also defines artifacts like validators and converters, each with a well-defined purpose. The end result is a very modular user interface code model that is easy to maintain, familiar to GUI application developers, and lends itself well to tools support.