[ Pobierz całość w formacie PDF ]

the cardemo application s AreaTag class:
public String getRendererType() { return  Area ;}
Register the Component
After writing your component classes, you need to register them with the appli-
cation using the application configuration file (see Application
Configuration (page 29))
Here are the declarations that register the UIMap and UIArea components:
Area
components.components.UIArea
Map
components.components.UIMap
The component-type element indicates the name under which the component
should be registered. Other objects referring to this component use this name.
The component-class element indicates the fully-qualified class name of the
component.
HANDLING EVENTS FOR CUSTOM COMPONENTS 141
Handling Events for Custom
Components
As explained in Handling Events (page 99), 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 (page 135) explained how to write the decode method of
UIMap to queue an event on the FacesContext component. This section explains
how to write an event handler to handle this event and to register the event han-
dler on the component.
The JavaServer Faces implementation calls the processing methods of any event
handlers registered on components and queued on theFacesContext. TheUIMap
component queues an event on the FacesContext. In the JSP page, the
ImageMapEventHandlerwas registered onmapby nesting theaction_listener
tag within the map tag:
...
Since ImageMapEventHandler is registered on the map component, the JavaSer-
ver Faces implementation calls the ImageMapEventHandler s processAction
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.
142 CREATING CUSTOM UI COMPONENTS
This method first gets the UIMap component that generated the event by calling
event.getSource. From this component, this method gets the currentArea
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 the FacesContext. The rest of the code sets the component tree in FacesCon-
text to that corresponding to Storefront.jsp, causing Storefront.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.
Using the Custom Component in the
Page
After you ve created your custom component and written all the accompanying
code, you are ready to use the component from the page.
To use the custom component in the JSP page, you need to declare the custom
tag library that defines the custom tag corresponding to the custom component.
The tag library is described in Defining the Custom Component Tag in a Tag
Library Descriptor (page 129).
To declare the custom tag library, include a taglib directive at the top of each
page that will contain the tags included in the tag library. Here is the taglib
directive that declares the JavaServer Faces components tag library:
prefix= d %>
The uri attribute value uniquely identifies the tag library. The prefix attribute
value is used to distinguish tags belonging to the tag library. For example, the
map tag must be referenced in the page with the d prefix, like this:
USING THE CUSTOM COMPONENT IN THE PAGE 143
Don t forget to also include the taglib directive for the standard tags included
with the RI:
When you reference any JavaServer Faces tags custom or standard from
within a JSP page, you must enclose all of them in the use_faces tag:
... other faces tags, custom tags, and possibly mixed with
other content
All form elements must also be enclosed within the form tag, which is also
nested within the use_faces tag:
... other faces tags, custom tags, and possibly mixed with
other content
The form tag encloses all of the controls that display or collect data from the
user. The formName attribute is passed to the application, where it is used to
select the appropriate business logic.
Now that you ve set up your page, you can add the custom tags in between the
form tags, as shown here in the ImageMap.jsp page:
...
prefix="d" %>
...
onmouseover= /cardemo/world_namer.jpg
144 CREATING CUSTOM UI COMPONENTS
onmouseout= /cardemo/world.jpg />
...
Conclusion
JavaServer Faces technology provides a rich, flexible architecture that makes it
easy to build Web applications with server-side UI functionality.
You have seen how to use this technology to extend the functionality of standard
components and create new components, to perform data conversions and vali-
dation, and to handle component events. You have also seen how to specify the
rendering of components and how to use them in a Web application.
You have gained this knowledge by learning about the various examples
included in the release and explained in this tutorial. You now have the means to
create your own Web applications using JavaServer Faces technology. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • sportingbet.opx.pl