7.4 User Interface Component Model
In addition to the lifecycle description, an overview of JavaServer Faces architecture provides better understanding of the technology.
JavaServer Faces components are the building blocks of a JavaServer Faces view. A component can be a user interface (UI) component or a non-UI component.
JavaServer Faces UI components are configurable, reusable elements that compose the user interfaces of JavaServer Faces applications. A component can be simple, such as a button, or can be compound, such as a table composed of multiple components.
JavaServer Faces technology provides a rich, flexible component architecture that includes the following:
This section briefly describes each of these pieces of the component architecture.
7.4.1 User Interface Component Classes
JavaServer Faces technology provides a set of UI component classes and associated behavioral interfaces that specify all the UI component functionality, such as holding component state, maintaining a reference to objects, and driving event handling and rendering for a set of standard components.
The component classes are completely extensible, allowing component writers to create their own custom components. See Chapter 15, "Creating Custom UI Components and Other Custom Objects" for more information.
The abstract base class for all components is
javax.faces.component.UIComponent. JavaServer Faces UI component classes extend the
UIComponentBase class (a subclass of
UIComponent), which defines the default state and behavior of a component. The following set of component classes is included with JavaServer Faces technology.
UIForm: Represents an input form to be presented to the user. Its child components represent (among other things) the input fields to be included when the form is submitted. This component is analogous to the
formtag in HTML.
UIViewParameter: Represents the query parameters in a request. This class is a subclass of the
In addition to extending
UIComponentBase, the component classes also implement one or more behavioral interfaces, each of which defines certain behavior for a set of components whose classes implement the interface.
These behavioral interfaces, all defined in the
javax.faces.component package unless otherwise stated, are as follows.
ActionSource: Indicates that the component can fire an action event. This interface is intended for use with components based on JavaServer Faces technology 1.1_01 and earlier versions. This interface is deprecated in JavaServer Faces 2.
ActionSourceand therefore provides the same functionality. However, it allows components to use the Expression Language (EL) when they are referencing methods that handle action events.
UIOutput and component classes that extend
UIInput and component classes that extend
Only component writers will need to use the component classes and behavioral interfaces directly. Page authors and application developers will use a standard component by including a tag that represents it on a page. Most of the components can be rendered in different ways on a page. For example, a
UICommand component can be rendered as a button or a link.
The next section explains how the rendering model works and how page authors can choose to render the components by selecting the appropriate tags.
7.4.2 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 class. This design has several benefits, including the following.
Component writers can define the behavior of a component once but create multiple renderers, each of which defines a different way to render the component to the same client or to different clients.
Page authors and application developers can change the appearance of a component on the page by selecting the tag that represents the appropriate combination of component and renderer.
A render kit defines how component classes map to component tags that are appropriate for a particular client. The JavaServer Faces implementation includes a standard HTML render kit for rendering to an HTML client.
The render kit defines a set of
javax.faces.render.Renderer classes for each component that it supports. Each
Renderer class defines a different way to render the particular component to the output defined by the render kit. For example, a
UISelectOne component has three different renderers. One of them renders the component as a group of options. Another renders the component as a combo box. The third one renders the component as a list box. Similarly, a
UICommand component can be rendered as a button or a link, using the
h:commandLink tag. The
command part of each tag corresponds to the
UICommand class, specifying the functionality, which is to fire an action. The
Link part of each tag corresponds to a separate
Renderer class that defines how the component appears on the page.
The section Adding Components to a Page Using HTML Tag Library Tags lists all supported component tags and illustrates how to use the tags in an example.
The JavaServer Faces implementation provides a custom tag library for rendering components in HTML.
A JavaServer Faces application can optionally associate a component with server-side object data. This object is a JavaBeans component, such as a managed bean. An application gets and sets the object data for a component by calling the appropriate object properties for that component.
The model view, in which data is represented as data types, such as
The presentation view, in which data is represented in a manner that can be read or modified by the user. For example, a
java.util.Datemight be represented as a text string in the format
mm/dd/yyor as a set of three text strings.
The JavaServer Faces implementation automatically converts component data between these two views when the bean property associated with the component is of one of the types supported by the component's data. For example, if a
UISelectBoolean component is associated with a bean property of type
java.lang.Boolean, the JavaServer Faces implementation will automatically convert the component's data from
Boolean. In addition, some component data must be bound to properties of a particular type. For example, a
UISelectBoolean component must be bound to a property of type
Sometimes you might want to convert a component's data to a type other than a standard type, or you might want to convert the format of the data. To facilitate this, JavaServer Faces technology allows you to register a
javax.faces.convert.Converter implementation on
UIOutput components and components whose classes subclass
UIOutput. If you register the
Converter implementation on a component, the
Converter implementation converts the component's data between the two views.
You can either use the standard converters supplied with the JavaServer Faces implementation or create your own custom converter. Custom converter creation is covered in Chapter 15, "Creating Custom UI Components and Other Custom Objects".
The JavaServer Faces event and listener model is similar to the JavaBeans event model in that it has strongly typed event classes and listener interfaces that an application can use to handle events generated by components.
The JavaServer Faces specification defines three types of events: application events, system events, and data-model events.
Application events are tied to a particular application and are generated by a
UIComponent. They represent the standard events available in previous versions of JavaServer Faces technology.
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 must 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 application events: action events and value-change events.
A value-change event (class
javax.faces.event.ValueChangeEvent) occurs when the user changes the value of a component represented by
UIInput or one of its subclasses. An example is selecting a check box, an action that results in the component's value changing to
true. The component types that can generate these types of events are the
UISelectBoolean components. Value-change events are fired only if no validation errors are detected.
Depending on the value of the
immediate property (see The immediate Attribute) of the component emitting the event, action events can be processed during the Invoke Application phase or the Apply Request Values phase, and value-change events can be processed during the Process Validations phase or the Apply Request Values phase.
System events are generated by an
Object rather than a
UIComponent. They are generated during the execution of an application at predefined times. They are applicable to the entire application rather than to a specific component.
A data-model event occurs when a new row of a
UIData component is selected.
There are two ways to cause your application to react to action events or value-change events that are emitted by a standard component:
See Referencing a Managed Bean Method for information on how to refer to the managed bean method from the component tag.
When emitting events from custom components, you must implement the appropriate event class and manually queue the event on the component in addition to implementing an event listener class or a managed bean method that handles the event. Handling Events for Custom Components explains how to do this.
JavaServer Faces technology supports a mechanism for validating the local data of editable components (such as text fields). This validation occurs before the corresponding model data is updated to match the local value.
Like the conversion model, the validation model defines a set of standard classes for performing common data validation checks. The JavaServer Faces core tag library also defines a set of tags that correspond to the standard
javax.faces.validator.Validator implementations. See Using the Standard Validators for a list of all the standard validation classes and corresponding tags.
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.
In addition to validators that are registered on the component, you can declare a default validator that is registered on all
UIInput components in the application. For more information on default validators, see Using Default Validators.
The validation model also allows you to create your own custom validator and corresponding tag to perform custom validation. The validation model provides two ways to implement custom validation.
Implement a managed bean method that performs the validation.
If you are implementing a
Validator interface, you must also do the following.
Validatorimplementation with the application.
In the previously described standard validation model, the validator is defined for each input component on a page. The Bean Validation model allows the validator to be applied to all fields in a page. See Chapter 21, "Introduction to Bean Validation" and Chapter 22, "Bean Validation: Advanced Topics" for more information on Bean Validation.