10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Steps for Creating a Custom Component<br />

<strong>The</strong> Duke's Bookstore source directory, called bookstore-dir,is<br />

tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/.<br />

<strong>The</strong> event and listener classes are located in bookstore-dir/listeners/. <strong>The</strong> tag handlers are<br />

located in bookstore-dir/taglib/. <strong>The</strong> component classes are located in<br />

bookstore-dir/components/. <strong>The</strong> renderer classes are located in bookstore-dir/renderers/.<br />

ImageArea is located in bookstore-dir/model/. LocaleBean is located in<br />

bookstore-dir/backing/.<br />

Steps for Creating a Custom Component<br />

416<br />

You can apply the following steps while developing your own custom component.<br />

1. Create a custom component class that does the following:<br />

a. Overrides the getFamily method to return the component family, which is used to look<br />

up renderers that can render the component.<br />

b. Includes the rendering code or delegates it to a renderer (explained in step 2).<br />

c. Enables component attributes to accept expressions.<br />

d. Queues an event on the component if the component generates events.<br />

e. Saves and restores the component state.<br />

2. Delegate rendering to a renderer if your component does not handle the rendering. To do<br />

this:<br />

a. Create a custom renderer class by extending javax.faces.render.Renderer.<br />

b. Register the renderer to a render kit.<br />

c. Identify the renderer type in the component tag handler.<br />

3. Register the component.<br />

4. Create an event handler if your component generates events.<br />

5. Write a tag handler class that extends javax.faces.webapp.UIComponentELTag. In this<br />

class, you need a getRendererType method, which returns the type of your custom renderer<br />

if you are using one (explained in step 2); a getComponentType method, which returns the<br />

type of the custom component; and a setProperties method, with which you set all the<br />

new attributes of your component.<br />

6. Create a tag library descriptor (TLD) that defines the custom tag.<br />

<strong>The</strong> application architect does the work of registering the custom component and the renderer.<br />

See “Registering a Custom Converter” on page 448 and “Registering a Custom Renderer with a<br />

Render Kit” on page 452 for more information. “Using a Custom Component” on page 374<br />

discusses how to use the custom component in a <strong>Java</strong>Server Faces page.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!