Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
Handling Events for Custom Components
As explained in Handling Events (page 884), a standard component queues events automatically on the
FacesContext
. Custom components on the other hand must manually queue the event from the decode method.Performing Decoding explained how to write the
decode
method ofUIMap
to queue an event on theFacesContext
component. This section explains how to write an event handler to handle this event and to register the event handler on the component.The JavaServer Faces implementation calls the processing methods of any event handlers registered on components and queued on the
FacesContext
. TheUIMap
component queues an event on theFacesContext
. In the JSP page, theImageMapEventHandler
is registered onmap
because theaction_listener
tag is nested within themap
tag:<d:map id="worldMap" currentArea="NAmericas" > <f:action_listener type="cardemo.ImageMapEventHandler"/> ... </d:map>Since
ImageMapEventHandler
is registered on themap
component, the JavaServer Faces implementation calls theImageMapEventHandler
'sprocessAction
method when the user clicks on the image map:public void processAction(ActionEvent event) { UIMap map = (UIMap)event.getSource(); String value = (String) map.getAttribute("currentArea"); Locale curLocale = (Locale) localeTable.get(value); if ( curLocale != null) { FacesContext context = FacesContext.getCurrentInstance(); context.setLocale(curLocale); String treeId = "/Storefront.jsp"; TreeFactory treeFactory = (TreeFactory) FactoryFinder.getFactory(FactoryFinder.TREE_FACTORY); Assert.assert_it(null != treeFactory); context.setTree(treeFactory.getTree(context,treeId)); } }When the JavaServer Faces implementation calls this method, it passes in an
ActionEvent
, representing the event generated by clicking on the image map. This method first gets theUIMap
component that generated the event by callingevent.getSource
. From this component, this method gets thecurrentArea
attribute value, which is the ID of the currently-selected area. With this value, this method gets the locale corresponding to the selected area and sets the locale in theFacesContext
. The rest of the code sets the component tree inFacesContext
to that corresponding toStorefront.jsp
, causingStorefront.jsp
to load after the user clicks the image map.It is possible to implement event-handling code in the custom component class instead of in an event handler if the component receives application events. This component class must subclass
UIComponentBase
. It must also implement the appropriate listener interface. This scenario allows an application developer to create a component that registers itself as a listener so that the page author doesn't need to register it.
Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.