Attribute Validation
In the previous example, the
UsePropertyTagExtraInfo
class sets the
varName
and className
properties of the VariableInfo
bean to the values
of the id
and className
attributes specified by the page author in the JSP page. This is done
using another simple class named TagData
, passed
as the argument to the getVariableInfo()
method.
The TagData
instance is created by the web
container to provide the TagExtraInfo
subclass
with information about all the action attributes specified by the
page author in the JSP page.
A TagData
instance is also passed as an argument
to the TagExtraInfo
isValid()
method. This method is called by the web container during the
translation phase to allow you to implement validation rules for the
custom action’s attributes. The container can perform simple
validation based on the information available in the TLD about which
attributes are required. But a custom action may have optional
attributes that are mutually exclusive or that depend on each other.
That’s when you have to implement the
isValid()
method in a
TagExtraInfo
subclass and provide your own
validation code.
The TagData
class has two methods of interest. The
getAttributeString()
method simply returns the
specified attribute as a String
. But some
attributes’ values may be specified by a JSP expression—a
so-called request-time attribute—instead of a string literal.
Since such a value is not known during the translation phase, the
TagData
class provides the
getAttribute()
method ...
Get JavaServer Pages Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.