background image

Steps for Creating a Custom Component

<< Summary of the Application Classes | Creating Custom Component Classes >>
<< Summary of the Application Classes | Creating Custom Component Classes >>

Steps for Creating a Custom Component

The Duke's Bookstore source directory, called bookstore-dir, is
tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/.
The event and listener classes are located in bookstore-dir/listeners/. The tag handlers are
located in bookstore-dir/taglib/. The component classes are located in
bookstore-dir/components/. The renderer classes are located in bookstore-dir/renderers/.
ImageArea
is located in bookstore-dir/model/. LocaleBean is located in
bookstore-dir/backing/.
Steps for Creating a Custom Component
You can apply the following steps while developing your own custom component.
1. Create a custom component class that does the following:
a. Overrides the getFamily method to return the component family, which is used to look
up renderers that can render the component.
b. Includes the rendering code or delegates it to a renderer (explained in step 2).
c. Enables component attributes to accept expressions.
d. Queues an event on the component if the component generates events.
e. Saves and restores the component state.
2. Delegate rendering to a renderer if your component does not handle the rendering. To do
this:
a. Create a custom renderer class by extending javax.faces.render.Renderer.
b. Register the renderer to a render kit.
c. Identify the renderer type in the component tag handler.
3. Register the component.
4. Create an event handler if your component generates events.
5. Write a tag handler class that extends javax.faces.webapp.UIComponentELTag. In this
class, you need a getRendererType method, which returns the type of your custom renderer
if you are using one (explained in step 2); a getComponentType method, which returns the
type of the custom component; and a setProperties method, with which you set all the
new attributes of your component.
6. Create a tag library descriptor (TLD) that defines the custom tag.
The application architect does the work of registering the custom component and the renderer.
See
"Registering a Custom Converter" on page 451
and
"Registering a Custom Renderer with a
Render Kit" on page 455
for more information.
"Using a Custom Component" on page 376
discusses how to use the custom component in a JavaServer Faces page.
Steps for Creating a Custom Component
The Java EE 5 Tutorial · September 2007
418