Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

How can a JSP Tag discover its own name?

Thread view: 
robin - 16 Aug 2007 22:09 GMT
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 - 16 Aug 2007 23:07 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> 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.

Signature

Lew

robin - 20 Aug 2007 18:42 GMT
> > Hi,
>
[quoted text clipped - 19 lines]
> 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 - 20 Aug 2007 19:00 GMT
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.

> 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
[quoted text clipped - 6 lines]
>
> -Robin Barooah
robin - 20 Aug 2007 22:08 GMT
> Robin,
>
[quoted text clipped - 13 lines]
>
> > -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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.