How can a JSP Tag discover its own name?

R

robin

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
 
L

Lew

robin said:
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.
 
R

robin

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?
 
S

sentientholon

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.
 
R

robin

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top