Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How can a JSP Tag discover its own name? (http://www.velocityreviews.com/forums/t529751-how-can-a-jsp-tag-discover-its-own-name.html)

robin 08-16-2007 09:09 PM

How can a JSP Tag discover its own name?
 
Hi,

I'm trying to write a JSP Tag handler class that can be used with a
number of different tags - in other words multiple <tag> entries in
the TLD would point to the same <tag-class>.

The reason I'm doing this is that I want to be able to use the
<jsp:attribute> with a few standard html tags, so I'm trying to build
a pass-through tag that will just replace itself an html tag with the
same attributes and body as it has.

In order to work properly, the tag handler needs to know what tag name
caused it to be invoked at run-time. I've looked long and hard at the
API documentation and the JSP 2.1 spec, but so far haven't seen a way
to get this information.

Am I missing something obvious?

Thanks for any help.

-Robin Barooah


Lew 08-16-2007 10:07 PM

Re: How can a JSP Tag discover its own name?
 
robin wrote:
> Hi,
>
> I'm trying to write a JSP Tag handler class that can be used with a
> number of different tags - in other words multiple <tag> entries in
> the TLD would point to the same <tag-class>.
>
> The reason I'm doing this is that I want to be able to use the
> <jsp:attribute> with a few standard html tags, so I'm trying to build
> a pass-through tag that will just replace itself an html tag with the
> same attributes and body as it has.
>
> In order to work properly, the tag handler needs to know what tag name
> caused it to be invoked at run-time. I've looked long and hard at the
> API documentation and the JSP 2.1 spec, but so far haven't seen a way
> to get this information.
>
> Am I missing something obvious?


Yeah, the fact that really you have a number of different tags, and therefore
should have a number of different tag implementation classes. If you're
switching on something in a class to determine behavior, that's a dead
giveaway that you need separate classes, usually subclasses of a common parent
with a polymorphic method that represents what would've been your switch cases.

--
Lew

robin 08-20-2007 05:42 PM

Re: How can a JSP Tag discover its own name?
 
On Aug 16, 3:07 pm, Lew <l...@lewscanon.com> wrote:
> robin wrote:
> > Hi,

>
> > I'm trying to write a JSP Tag handler class that can be used with a
> > number of different tags - in other words multiple <tag> entries in
> > the TLD would point to the same <tag-class>.

>
> > The reason I'm doing this is that I want to be able to use the
> > <jsp:attribute> with a few standard html tags, so I'm trying to build
> > a pass-through tag that will just replace itself an html tag with the
> > same attributes and body as it has.

>
> > In order to work properly, the tag handler needs to know what tag name
> > caused it to be invoked at run-time. I've looked long and hard at the
> > API documentation and the JSP 2.1 spec, but so far haven't seen a way
> > to get this information.

>
> > Am I missing something obvious?

>
> Yeah, the fact that really you have a number of different tags, and therefore
> should have a number of different tag implementation classes. If you're
> switching on something in a class to determine behavior, that's a dead
> giveaway that you need separate classes, usually subclasses of a common parent
> with a polymorphic method that represents what would've been your switch cases.
>


Normally, I'd agree with this kind of reasoning (and therefore
wouldn't have posted!) I totally agree that a switch is usually a code
smell (unless you're classifying input data from outside the system,
which I am not here). However, in this case, I'm not switching on the
tag name - I'm just passing it through to the output. The behavior is
identical in each case, but the output is a function of the tag name,
hence my desire to use it as a parameter.

If I do create a number of different implementation classes, I'll end
up with an abstract base class with all of the functionality in it,
and a subclass for each taq. The subclasses will only contain one
overridden method:

protected String getTagName();

Which will return a string literal of the tag name.

Granted, this is not the worst thing in the world, however I was
hoping there was a way to avoid resorting to this kind of boilerplate.

Do you know if there's any other way to use tags inside attributes of
tags that are not jsp actions or tag libraries?

> --
> Lew




sentientholon@gmail.com 08-20-2007 06:00 PM

Re: How can a JSP Tag discover its own name?
 
Robin,

Nope, you're not missing anything. :-) The mapping between tag name
and tag class occurs when the JSP compiler parses the TLD, and
instantiates the JSP java class. The tag object itself has no
knowledge of this information.

On Aug 16, 4:09 pm, robin <robi...@gmail.com> wrote:
> In order to work properly, the tag handler needs to know what tag name
> caused it to be invoked at run-time. I've looked long and hard at the
> API documentation and the JSP 2.1 spec, but so far haven't seen a way
> to get this information.
>
> Am I missing something obvious?
>
> Thanks for any help.
>
> -Robin Barooah




robin 08-20-2007 09:08 PM

Re: How can a JSP Tag discover its own name?
 
On Aug 20, 11:00 am, sentientho...@gmail.com wrote:
> Robin,
>
> Nope, you're not missing anything. :-) The mapping between tag name
> and tag class occurs when the JSP compiler parses the TLD, and
> instantiates the JSP java class. The tag object itself has no
> knowledge of this information.
>
> On Aug 16, 4:09 pm, robin <robi...@gmail.com> wrote:
>
> > In order to work properly, the tag handler needs to know what tag name
> > caused it to be invoked at run-time. I've looked long and hard at the
> > API documentation and the JSP 2.1 spec, but so far haven't seen a way
> > to get this information.

>
> > Am I missing something obvious?

>
> > Thanks for any help.

>
> > -Robin Barooah


Thanks to both of you. I've gone down the path of creating an
abstract base class to do the pass through, and then creating a final
concrete subclass if it with just a function returning the tag name
for each actual tag I want to override.




All times are GMT. The time now is 05:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.