[ Pobierz całość w formacie PDF ]

" If you need to aggregate components to create a new component that has
its own unique behavior. The new component must be a custom compo-
nent. One example is a datechooser component consisting of three drop-
down lists.
" If you need a component that is supported by an HTML client, but is not
currently implemented by JavaServer Faces technology. The current
release does not contain standard components for complex HTML compo-
nents, like frames; however, because of the extensibility of the component
architecture, you can easily create components like this.
" If you need to render to a non-HTML client, which requires extra compo-
nents not supported by HTML. Eventually, the standard HTML render kit
will provide support for all standard HTML components. However, if you
are rendering to a different client such as a phone you might need to
create custom components to represent the controls uniquely supported by
the client. For example, the MIDP component architecture includes sup-
port for tickers and progress bars, which are not available on an HTML cli-
ent. In this case, you might also need a custom renderer along with the
component; or, you might just need a custom renderer.
WHEN TO USE A CUSTOM RENDERER 119
You do not need to create a custom component if:
" You need to simply manipulate data on the component or add application-
specific functionality to it. In this situation, you should create a model
object for this purpose and bind it to the standard component rather than
create a custom component. See Writing a Model Object Class (page 75)
for more information on creating a model object.
" You need to convert a component s data to a type not supported by its ren-
derer. See Performing Data Conversions (page 92) for more information
about converting a component s data.
" You need to perform validation on the component data. Both standard val-
idators and custom validators can be added to a component by using the
validator tags from the page. See Performing Validation (page 81) for
more information about validating a component s data.
" You need to register event listeners on components. The EA3 release elim-
inated the need to create a custom component in order to register an event
listener on it. Now you can register event listeners on components with the
valuechanged_event and action_listener tags. See Handling
Events (page 99) for more information on using these tags.
When to Use a Custom Renderer
If you are creating a custom component, you need to ensure among other
things that your component class performs these operations:
" Decoding: converting the incoming request parameters to the local value
of the component.
" Encoding: converting the current local value of the component into the cor-
responding markup that represents it in the response.
The JavaServer Faces specification supports two programming models for han-
dling encoding and decoding:
" Direct implementation: The component class itself implements the decod-
ing and encoding.
" Delegated implementation: The component class delegates the implemen-
tation of encoding and decoding to a separate renderer
By delegating the operations to the renderer, you have the option of associating
your custom component with different renderers so that you can represent the
component in different ways on the page. If you don t plan to render a particular
120 CREATING CUSTOM UI COMPONENTS
component in different ways, it s simpler to let the component class handle the
rendering.
If you aren t sure if you will need the flexibility offered by separate renderers,
but want to use the simpler direct implementation approach, you can actually use
both models. Your component class can include some default rendering code, but
it can delegate rendering to a renderer if there is one.
Component, Renderer, and Tag
Combinations
When you decide to create a custom component, you will usually create a cus-
tom renderer to go with it. You will also need a custom tag to associate the com-
ponent with the renderer and to reference the component from the page.
In rare situations, however, you might use a custom renderer with a standard
component rather than a custom component. Or, you might use a custom tag
without a renderer or a component. This section gives examples of these situa-
tions and provides a summary of what s required for a custom component, ren-
derer, and tag.
One example of using a custom renderer without a custom component is when
you want to add some client-side validation on a standard component. You
would implement the validation code with a client-side scripting language, such
as JavaScript. You render the JavaScript with the custom renderer. In this situa-
tion, you will need a custom tag to go with the renderer so that its tag handler
can register the renderer on the standard component.
Both custom components and custom renderers need custom tags associated
with them. However, you can have a custom tag without a custom renderer or
custom component. One example is when you need to create a custom validator
that requires extra attributes on the validator tag. In this case, the custom tag cor-
responds to a custom validator, not to a custom component or custom renderer.
In any case, you still need to associate the custom tag with a server-side object.
UNDERSTANDING THE IMAGE MAP EXAMPLE 121
The following table summarizes what you must or can associate with a custom
component, custom renderer, or custom tag.
Table 4 1 Requirements for Custom Components, Custom Renderers, and Custom
Tags
Must have Can have
custom component custom tag custom renderer
custom component or standard com-
custom renderer
custom tag
ponent
some server-side object, like custom component or standard com-
custom JavaServer
a component, a custom ren- ponent associated with a custom
Faces tag
derer, or custom validator renderer
Understanding the Image Map
Example
The cardemo application now includes a custom image map component on the
ImageMap.jsp page. This image map displays a map of the world. When the
user clicks on one of a particular set of regions in the map, the application sets
the locale in the FacesContext to the language spoken in the selected region.
The hot spots of the map are: the United States, Spanish-speaking Central and
South America, France, and Germany.
Why Use JavaServer Faces Technology
to Implement an Image Map?
JavaServer Faces technology is an ideal framework to use for implementing this
kind of image map because it can perform the work that must be done on the
server without requiring you to create a server-side image map.
In general, client-side image maps are preferred over server-side image maps for
a few reasons. One reason is that the client-side image map allows the browser to
provide immediate feedback when a user positions her mouse over a hot spot.
Another reason is that client-side image maps perform better because they don t
122 CREATING CUSTOM UI COMPONENTS
require round-trips to the server. However, in some situations, your image map
might need to access the server to retrieve some data or to change the appearance
of non-form controls, which a client-side image map cannot do.
The image map custom component because it uses JavaServer Faces technol-
ogy has the best of both style of image maps: It can handle the parts of the
application that need to be performed on the server, while allowing the other [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • gim1chojnice.keep.pl