Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

User Interface Component Model

JavaServer Faces UI components are configurable, reusable elements that compose the user interfaces of JavaServer Faces applications. A component can be simple, like a button, or compound, like a table, which can be composed of multiple components.

JavaServer Faces technology provides a rich, flexible component architecture that includes:

This section briefly describes each of these pieces of the component architecture.

The User-Interface Component Classes

JavaServer Faces technology provides a set of UI component classes, which specify all of the UI component functionality, such as holding component state, maintaining a reference to model objects, and driving event-handling and rendering for a set of standard components.

These classes are completely extensible, allowing component writers to create their own custom components. See Creating Custom UI Components (page 903) for an example of a custom image map component.

All JavaServer Faces UI component classes extend from UIComponentBase, which defines the default state and behavior of a UIComponent. The set of UI component classes included in this release of JavaServer Faces are:

Most page authors and application developers will not have to use these classes directly. They will instead include the components on a page by using the component's corresponding tag. Most of these component tags can be rendered in different ways. For example, a UICommand can be rendered as a button or a hyperlink.

The next section explains how the rendering model works and how page authors choose how to render the components by selecting the appropriate tag.

The Component Rendering Model

The JavaServer Faces component architecture is designed such that the functionality of the components is defined by the component classes, whereas the component rendering can be defined by a separate renderer. This design has several benefits including:

A render kit defines how component classes map to component tags appropriate for a particular client. The JavaServer Faces implementation includes a standard RenderKit for rendering to an HTML client.

For every UI component that a RenderKit supports, the RenderKit defines a set of Renderer objects. Each Renderer defines a different way to render the particular component to the output defined by the RenderKit. For example, a UISelectOne component has three different renderers. One of them renders the component as a set of radio buttons. Another renders the component as a combo box. The third one renders the component as a list box.

Each JSP custom tag in the standard HTML RenderKit is composed of the component functionality, defined in the UIComponent class, and the rendering attributes, defined by the Renderer. For example, the two tags in Table 20-1 both represent a UICommand component, rendered in two different ways:

Table 20-1 UICommand Tags 
Tag
Rendered as
command_button

Figure 20-4

Login Button
Login Button

command_hyperlink

Figure 20-5

A Hyperlink
A Hyperlink

The command part of the tags corresponds to the UICommand class, specifying the functionality, which is to fire an action. The button and hyperlink parts of the tags each correspond to a separate Renderer, which defines how the component appears on the page.

The JavaServer Faces reference implementation provides a custom tag library for rendering components in HTML. It supports all of the component tags listed in Table 20-2. To learn how to use the tags in an example, see Using the JavaServer Faces Tag Libraries (page 835).

Table 20-2 The Component Tags 
Tag
Functions
Rendered as
Appearance
command_button
Submits a form to the application.
An HTML
<input type=type>
element, where the type value can be submit, reset, or image
A button
command_hyperlink
Links to another page or location on a page.
An HTML <a href> element
A Hyperlink
form
Represents an input form. The inner tags of the form receive the data that will be submitted with the form.
An HTML <form>
element
No appearance
graphic_image
Displays an image.
An HTML <img>
element
An image
input_date
Allows a user to enter a date.
An HTML
<input type= text>
element
A text string, formatted with a java.text.
DateFormat
date instance
input_datetime
Allows a user to enter a date and time.
An HTML
<input type=text>
element
A text string, formatted with a java.text.
SimpleDateFormat
datetime instance
input_hidden
Allows a page author to include a hidden variable in a page.
An HTML
<input type=hidden> element
No appearance
input_number
Allows a user to enter a number.
An HTML
<input type=text> element
A text string, formatted with a java.text.
NumberFormat
instance
input_secret
Allows a user to input a string without the actual string appearing in the field.
An HTML <input type=password> element
A text field, which displays a row of characters instead of the actual string entered
input_text
Allows a user to input a string.
An HTML <input type=text> element
A text field
input_textarea
Allows a user to enter a multi-line string.
An HTML <textarea> element
A multi-row text field
input_time
Allows a user to enter a time.
An HTML <input type=text> element
A text string, formatted with a java.text.
DateFormat
time instance
output_date
Displays a formatted date.
plain text
A text string, formatted with a java.text.
DateFormat
time instance
output_datetime
Displays a formatted date and time.
plain text
A text string, formatted with a java.text.
SimpleDateFormat
datetime instance
output_errors
Displays error messages.
plain text
plain text
output_label
Displays a nested component as a label for a specified input field.
An
HTML <label> element
plain text
output_message
Displays a localized message.
plain text
plain text
output_number
Displays a formatted number.
plain text
A text string, formatted with a java.text.
NumberFormat
instance
output_text
Displays a line of text.
plain text
plain text
output_time
Displays a formatted time.
plain text
A text string, formatted with a java.text.
DateFormat
time instance
panel_data
Iterates over a collection of data.
 
A set of rows in a table
panel_grid
Displays a table.
An HTML <table> element with <tr> and <td> elements
A table
panel_group
Groups a set of components under one parent.
 
A row in a table
panel_list
Displays a table of data that comes from a collection, array, iterator, or map.
An HTML <table> element with <tr> and <td> elements
A table
selectboolean
_checkbox
 
Allows a user to change the value of a boolean choice.
An HTML <input type=checkbox> element.
A checkbox
selectitem
 
Represents one item in a list of items in a UISelectOne component.
An HTML <option> element
No appearance
selectitems
Represents a list of items in a UISelectOne component.
A list of HTML <option> elements
No appearance
selectmany
_checkboxlist
Displays a set of checkboxes, from which the user can select multiple values.
A set of HTML <input> elements of type checkbox
A set of checkboxes
selectmany
_listbox
Allows a user to select multiple items from a set of items, all displayed at once.
A set of HTML <select> elements
A list box
selectmany_menu
Allows a user to select multiple items from a set of items.
A set of HTML <select> elements
A scrollable combo box
selectone
_listbox
Allows a user to select one item from a set of items, all displayed at once.
A set of HTML <select> elements
 
A list box
selectone_menu
Allows a user to select one item from a set of items.
An HTML <select> element
A scrollable combo box
selectone_radio
Allows a user to select one item from a set of items.
An HTML <input type=radio> element
A set of radio buttons

Conversion Model

A JavaServer Faces application can optionally associate a component with server-side model object data. This model object is a JavaBeans component that encapsulates the data on a set of components. An application gets and sets the model object data for a component by calling the appropriate model object properties for that component.

When a component is bound to a model object, the application has two views of the component's data: the model view and the presentation view, which represents the data in a manner that can be viewed and modified by the user.

A JavaServer Faces application must ensure that the component's data can be converted between the model view and the presentation view. This conversion is usually performed automatically by the component's renderer.

In some situations, you might want to convert a component's data to a type not supported by the component's renderer. To facilitate this, JavaServer Faces technology includes a set of standard Converter implementations and also allows you to create your own custom Converter implementations. If you register the Converter implementation on a component, the Converter implementation converts the component's data between the two views. See Performing Data Conversions (page 878) for more details on the converter model, how to use the standard converters, and how to create and use your own custom converter.

Event and Listener Model

One goal of the JavaServer Faces specification is to leverage existing models and paradigms so that developers can quickly become familiar with using JavaServer Faces in their web applications. In this spirit, the JavaServer Faces event and listener model leverages the JavaBeans event model design, which is familiar to GUI developers and Web Application Developers.

Like the JavaBeans component architecture, JavaServer Faces technology defines Listener and Event classes that an application can use to handle events generated by UI components. An Event object identifies the component that generated the event and stores information about the event. To be notified of an event, an application must provide an implementation of the Listener class and register it on the component that generates the event. When the user activates a component, such as by clicking a button, an event is fired. This causes the JavaServer Faces implementation to invoke the listener method that processes the event.

JavaServer Faces supports two kinds of events: value-changed events and action events.

A value-changed event occurs when the user changes a component value. An example is selecting a checkbox, which results in the component's value changing to true. The component types that generate these types of events are the UIInput, UISelectOne, UISelectMany, and UISelectBoolean components. Value-changed events are only fired if no validation errors were detected.

An action event occurs when the user clicks a button or a hyperlink. The UICommand component generates this event.

For more information on handling these different kinds of events, see Handling Events (page 884).

Validation Model

JavaServer Faces technology supports a mechanism for validating a component's local data during the Process Validations phase, before model object data is updated.

Like the conversion model, the validation model defines a set of standard classes for performing common data validation checks. The jsf-core tag library also defines a set of tags that correspond to the standard Validator implementations.

Most of the tags have a set of attributes for configuring the validator's properties, such as the minimum and maximum allowable values for the component's data. The page author registers the validator on a component by nesting the validator's tag within the component's tag.

Also like the conversion model, the validation model allows you to create your own Validator implementation and corresponding tag to perform custom validation. See Performing Validation (page 867) for more information on the standard Validator implementations and how to create custom Validator implementation and validator tags.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.