Inside a Gracelet Component Library you can also define Facelet handlers.
Example Component Handler
If you want to instantiate a certain already existing component, but you would like
to manipulate it once its created and/or populated, or specify attribute auto wiring,
you can do so with a ComponentHandler.
With a ComponentHandler you must specify the component you want to instantiate. You can
pass a String which will be interpreted as a component-name defined in a faces-config.xml.
You can pass a component defined in the same library. Or you can pass a closure which will return
a UIComponent instance.
For instance, one of the optional libraries available is the MetaWidgetComponentLibrary, defined as follows:
Above we simply say that when the 'div' tag is called on this library, it should create an instance
of 'org.metawidget.HtmlMetawidget', and then once created it should set the 'rendererType' attribute
to 'div'. We do the same thing with the 'table' tag, except we specify the table
renderer instead of the div one.
Example Tag Handler
One of the main attractions for facelets is the ability to use source files that are templates
or parts of pages and that can be reused. In a Gracelet Component Library, it is as easy as creating
a gracelet tag to do the same without the need for artifacts (a taglib.xml).
For instance, in the main Gracelets Component Library that comes packaged with gracelets, there
is a form tag that is really a tag handler, its purpose is to make sure that the conversation id
is propogated, inherent to the form.
As can be seen you can start defining the tag just like in a gracelets view script,
no need to declare the common tag library namespaces. And you can do template inserting
so that it works just like a user tag source file.
If we used this then we would get the following results (before facelets sends it to the browser):
Sometimes it would be nice to use the tag name as some type of indicator, and then build
something off this indicator. With a 'proxy' tag you can do just that. You can define
as many components as you want in a library, but if you define a 'proxy' tag inside the same library
it will 'catch' all tags that do not exist. You will be provided with the name of the tag being
used via a special attribute so that you can use that for processing the tag.
The following code shows how this is being done with the wicket component library that comes
with the optional wicket extension:
As can be seen above this special tag must be called 'proxy'. Inside that you can get the special
attribute called 'gracelet.TagName' which will return the name of the tag called on this library but that
did not exist. Here we use this so that when a developer makes a Wicket Gracelet Page as a wicket template
then he can easily use tag calls instead of having to escape the tags since a ':' in a tag name
is not pretty when writing gracelets scripts (i.e., form("wicket:id": "someid")). The following example
and the output it generates will demonstrate this (taken from the Guest Book example):