|
Download
FAQ History |
|
API
Search Feedback |
Using Custom Objects
As a page author, you might need to use custom converters, validators, or components packaged with the application on your JSP pages.
A custom converter is applied to a component either by using the component tag's
converterattribute or by nesting anconvertertag inside the component's tag.A custom validator is applied to a component by nesting either a
validatortag or the validator's custom tag inside the component's tag.To use a custom component, you use the custom tag associated with the component.
As explained in Setting Up a Page, you must ensure that the TLD that defines the custom tags is packaged in the application. TLD files are stored in the
WEB-INFdirectory or subdirectory of the WAR file or in theMETA-INF/directory or subdirectory of a tag library packaged in a JAR.Next, you include a
taglibdeclaration so that the page has access to the tags. All custom objects for the Duke's Bookstore application are defined in thebookstore.tld. Here is thetaglibdeclaration that you would include on your page so that you can use the tags from this TLD:When including the custom tag in the page, you can consult the TLD to determine which attributes the tag supports and how they are used.
The next three sections describe how to use the custom converter, validator, and UI component included in the Duke's Bookstore application.
Using a Custom Converter
To apply the data conversion performed by a custom converter to a particular component's value, you must either set the
converterattribute of the component's tag to theConverterimplementation's identifier or set the nestedconvertertag'sconverterIdattribute to theConverterimplementation's identifier. The application architect provided this identifier when registering theConverterwith the application, as explained in Registering a Custom Converter. Creating a Custom Converter explains how the custom converter was implemented.The identifier for the
CreditCardConverteriscreditCardConverter. TheCreditCardConverteris attached to theccnocomponent, as shown in this tag from thebookcashier.jsppage:<h:inputText id="ccno" size="19" converter="creditCardConverter" required="true"> ... </h:inputText>By setting the
converterattribute of a component's tag to the identifier of aConverter, you cause that component's local value to be automatically converted according to the rules specified in theConverterimplementation.A page author can use the same custom converter with any similar component by simply supplying the
Converterimplementation's identifier to theconverterattribute of the component's tag or to theconvertIdattribute of the nestedconvertertag.Using a Custom Validator
To use a custom validator in a JSP page, you must nest the validator's custom tag inside the tag of the component whose value you want to be validated by the custom validator.
Here is the
formatValidatortag from theccnofield on thebookcashier.jsppage of the Duke's Bookstore application:<h:inputText id="ccno" size="19" ... required="true"> <bookstore:formatValidator formatPatterns="9999999999999999|9999 9999 9999 9999| 9999-9999-9999-9999" /> </h:inputText> <h:message styleClass="validationMessage" for="ccno"/>This tag validates the input of the
ccnofield against the patterns defined by the page author in theformatPatternsattribute.You can use the same custom validator for any similar component by simply nesting the custom validator tag within the component tag.
Creating a Custom Validator describes how to create the custom validator and its custom tag.
If the application developer who created the custom validator prefers to configure the attributes in the
Validatorimplementation rather than allow the page author to configure the attributes from the page, the developer will not create a custom tag for use with the validator.Instead, the page author must follow these steps:
- Nest the
validatortag inside the tag of the component whose data needs to be validated.- Set the
validatortag'svalidatorIdattribute to the ID of the validator that is defined in the application configuration resource file. Registering a Custom Validator explains how to define the validator in the application configuration resource file.The following tag registers the
FormatValidatoron a component using anvalidatortag and referencing the ID of the validator:<h:inputText id="zip" value="#{CustomerBean.zip}" size="10" ... > <f:validator validatorId="FormatValidator" /> ... </h:inputText>Using a Custom Component
Using a custom component on a page is similar to using a custom validator, except that custom validator tags must be nested inside component tags. In order to use the custom component in the page, you need to declare the tag library that defines the custom tags that render the custom component. This is explained in Using Custom Objects.
The Duke's Bookstore application includes a custom image map component on the
chooselocale.jsppage. This component allows you to select the locale for the application by clicking on a region of the image map:... <h:graphicImage id="mapImage" url="/template/world.jpg" alt="#{bundle.chooseLocale}" usemap="#worldMap" /> <bookstore:map id="worldMap" current="NAmericas" immediate="true" action="bookstore" actionListener="#{localeBean.chooseLocaleFromMap}"> <bookstore:area id="NAmerica" value="#{NA}" onmouseover="/template/world_namer.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" /> ... <bookstore:area id="France" value="#{fraA}" onmouseover="/template/world_france.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" /> </bookstore:map>The
graphicImagetag associates an image (world.jpg) with an image map that is referenced in theusemapattribute value.The
maptag specifies the image map and contains a set ofareatags. Eachareatag specifies a region of the image map.The
onmouseoverandonmouseoutattributes define the image that is displayed when the user performs the actions described by the attributes. The page author defines what these images are. The custom renderer also renders anonclickattribute.In the rendered HTML page, the
onmouseover,onmouseout, andonclickattributes define which JavaScript code is executed when these events occur. When the user moves the mouse over a region, theonmouseoverfunction associated with the region displays the map with that region highlighted. When the user moves the mouse out of a region, theonmouseoutfunction redisplays the original image. When the user clicks a region, theonclickfunction sets the value of a hiddeninputtag to the ID of the selected area and submits the page.When the custom renderer renders these attributes in HTML, it also renders the JavaScript code. The custom renderer also renders the entire
onclickattribute rather than let the page author set it.The custom renderer that renders the
maptag also renders a hiddeninputcomponent that holds the current area. The server-side objects retrieve the value of the hiddeninputfield and set the locale in theFacesContextaccording to which region was selected.Chapter 20 describes the custom tags in more detail and also explains how to create the custom image map components, renderers, and tags.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.