background image

Creating Custom Component Classes

<< Steps for Creating a Custom Component | The UIComponentBase >>
<< Steps for Creating a Custom Component | The UIComponentBase >>

Creating Custom Component Classes

Creating Custom Component Classes
As explained in
"When to Use a Custom Component" on page 410
, a component class defines
the state and behavior of a UI component. The state information includes the component's type,
identifier, and local value. The behavior defined by the component class includes the following:
Decoding (converting the request parameter to the component's local value)
Encoding (converting the local value into the corresponding markup)
Saving the state of the component
Updating the bean value with the local value
Processing validation on the local value
Queueing events
The UIComponentBase class defines the default behavior of a component class. All the classes
representing the standard components extend from UIComponentBase. These classes add their
own behavior definitions, as your custom component class will do.
Your custom component class must either extend UIComponentBase directly or extend a class
representing one of the standard components. These classes are located in the
javax.faces.component
package and their names begin with UI.
If your custom component serves the same purpose as a standard component, you should
extend that standard component rather than directly extend UIComponentBase. For example,
suppose you want to create an editable menu component. It makes sense to have this
component extend UISelectOne rather than UIComponentBase because you can reuse the
behavior already defined in UISelectOne. The only new functionality you need to define is to
make the menu editable.
Whether you decide to have your component extend UIComponentBase or a standard
component, you might also want your component to implement one or more of these
behavioral interfaces:
ActionSource
: Indicates that the component can fire an ActionEvent.
ActionSource2
: Extends ActionSource and allows component properties referencing
methods that handle action events to use method expressions as defined by the unified EL.
This class was introduced in JavaServer Faces Technology 1.2.
EditableValueHolder
: Extends ValueHolder and specifies additional features for editable
components, such as validation and emitting value-change events.
NamingContainer
: Mandates that each component rooted at this component have a unique
ID.
StateHolder
: Denotes that a component has state that must be saved between requests.
ValueHolder
: Indicates that the component maintains a local value as well as the option of
accessing data in the model tier.
Creating Custom Component Classes
Chapter 13 · Creating Custom UI Components
419