On 17 Jul 2002, David M. Karr wrote:

> I'm working on an experimental tag library that is an extension of
> Struts, where all the tags use the JSTL EL evaluator instead of using
> RT expressions.
> 
> In general, the subclasses will be quite simple boilerplate code, just
> requiring calls to "ExressionUtil.evalNotNull()" for each attribute
> that used to be an "rt" expression.
> 
> I've noticed one wrinkle, however.  I'm implementing the Struts
> "bean:size" tag, which has a "collection" attribute.  In most cases,
> each Struts tag attribute can be only one type.  However, the
> "collection" attribute of the "bean:size" tag can be either a "Map",
> "Collection", or an array of any type. I don't see an easy way to do
> this with the ExpressionUtil class, or even with the classes it uses
> (ExpressionEvaluatorManager and ExpressionEvaluator).
> 
> I suppose I could just use "Object.class", as the base "SizeTag" class
> validates the type of the expression.  What I'm unsure of, however, if
> whether specifying "Object.class" will work, allowing the
> specification of either a Map, Collection, or Array in the attribute
> EL expression.

Yes, specifying Object.class is appropriate in such cases.  (See, by way
of comparison, the source for <c:forEach> and how that tag handler handles
its 'items' attribute.)  It will allow the expression to resolve to any of
those types; it's incumbent upon you, however, to ensure that the object
you got was really one of the types you accept.  For instance, you'll
probably want to have a check following the call to evalNotNull() along
the lines of:

  if (!(collection instanceof Map) && . . .)
    throw new JspTagException("need a Map or a . . . ");

Otherwise, it should work just as you expect.

-- 
Shawn Bayern
"JSTL in Action"   http://www.jstlbook.com
(coming in July 2002 from Manning Publications)


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to