background image

Creating the Component Tag Handler

<< Handling Events for Custom Components | Getting the Attribute Values >>
<< Handling Events for Custom Components | Getting the Attribute Values >>

Creating the Component Tag Handler

method then uses the value of the current property to get the Locale object from a HashMap
object, which is constructed elsewhere in the LocaleBean class. Finally the method sets the
locale of the FacesContext instance to the Locale obtained from the HashMap object.
In addition to the method that processes the event, you need the event class itself. This class is
very simple to write: You have it extend ActionEvent and provide a constructor that takes the
component on which the event is queued and a method that returns the component. Here is the
AreaSelectedEvent
class used with the image map:
public class AreaSelectedEvent extends ActionEvent {
...
public AreaSelectedEvent(MapComponent map) {
super(map);
}
public MapComponent getMapComponent() {
return ((MapComponent) getComponent());
}
}
As explained in the section
"Creating Custom Component Classes" on page 419
, in order for
MapComponent
to fire events in the first place, it must implement ActionSource. Because
MapComponent
extends UICommand, it also implements ActionSource.
Creating the Component Tag Handler
Now that you've created your component and renderer classes, you're ready to define how a tag
handler processes the tag representing the component and renderer combination. If you've
created your own JSP custom tags before, creating a component tag handler should be easy for
you.
In JavaServer Faces applications, the tag handler class associated with a component drives the
render response phase of the JavaServer Faces life cycle. For more information on the JavaServer
Faces life cycle, see
"The Life Cycle of a JavaServer Faces Page" on page 313
.
The first thing that the tag handler does is to retrieve the type of the component associated with
the tag. Next, it sets the component's attributes to the values given in the page. It then returns
the type of the renderer (if there is one) to the JavaServer Faces implementation so that the
component's encoding can be performed when the tag is processed. Finally, it releases resources
used during the processing of the tag.
The image map custom component includes two tag handlers: AreaTag and MapTag. To see how
the operations on a JavaServer Faces tag handler are implemented, let's take a look at MapTag.
The MapTag class extends UIComponentELTag, which supports jsp.tagext.Tag functionality as
well as JavaServer Faces-specific functionality. UIComponentELTag is the base class for all
JavaServer Faces tags that correspond to a component. Tags that need to process their tag
bodies should instead subclass UIComponentBodyELTag.
Creating the Component Tag Handler
The Java EE 5 Tutorial · September 2007
430