|
Download
FAQ History |
|
API
Search Feedback |
Handling Events for Custom Components
As explained in Handling Events, 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
decodemethod ofUIMapto queue an event on theFacesContextcomponent. 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. TheUIMapcomponent queues an event on theFacesContext. In the JSP page, theImageMapEventHandleris registered onmapbecause theaction_listenertag is nested within themaptag:<d:map id="worldMap" currentArea="NAmericas" > <f:action_listener type="cardemo.ImageMapEventHandler"/> ... </d:map>Since
ImageMapEventHandleris registered on themapcomponent, the JavaServer Faces implementation calls theImageMapEventHandler'sprocessActionmethod 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 theUIMapcomponent that generated the event by callingevent.getSource. From this component, this method gets thecurrentAreaattribute 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 inFacesContextto that corresponding toStorefront.jsp, causingStorefront.jspto 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.