How to get the tag name from within a taglib class ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the tag name from within a taglib class ?

Rony G. Flatscher (Apache)
While exploring, experimenting with creating a taglib (implementing the BodyTag interface) I would
have a need to find out the tag name that caused the tagclass to run.

Is this possible? If so, how would one be able to get at that tag name (any brief hints would suffice) ?

---rony

P.S.: If possible I would like to write a single tagclass, but use it for two or more different
tags, as the implementation would share quite a lot of code. Besides, it might be helpful for debugging.




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to get the tag name from within a taglib class ?

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Rony,

On 9/11/20 10:28, Rony G. Flatscher (Apache) wrote:

> While exploring, experimenting with creating a taglib (implementing
> the BodyTag interface) I would have a need to find out the tag name
> that caused the tagclass to run.
>
> Is this possible? If so, how would one be able to get at that tag
> name (any brief hints would suffice) ?
>
> ---rony
>
> P.S.: If possible I would like to write a single tagclass, but use
> it for two or more different tags, as the implementation would
> share quite a lot of code. Besides, it might be helpful for
> debugging.

It seems like this is the wrong approach, unless you just want to use
this for something like logging.

I you want the tags to behave differently, then you should have
separate tag implementations. Feel free to build a base class with the
shared code and then implement the differences in subclasses.

It's been a loooong time since I wrote a custom tag library but I do
remember that the whole process was very painful and despite the
flexibility allowed by the API, lots of common things (like getting
the tag name!) were either awkward or impossible.

IMO, the JSP effort was a stepping-stone on a path to better
technologies like Velocity, FreeMarker, and others. If I were king,
JSP would just go away. Just my POV of course, you are welcome to fall
in love with JSP. :)

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl9btG8ACgkQHPApP6U8
pFiYyhAAxG/8tTbGEp9pN3RvxjvRQXf+UupG4S3CXQNoNeVr4ffpWWii0kLHLLpj
YoBpqNUjsl4Uki2jLADPioUHKdLOmVSlL6NTEcgMyEdIrCsI1A49okpnAIIX5EVB
SRbfiUnv+37zQrKn3HbNnW8p6OoANHaEpGyfP6ylv6nj85nm9eAmXeKy/BcQLrI3
qRPD4migbvxGSU5M36YbT74q4azYV0bXEDh/9M3+Ky6iuPGGe0H+Y6V/JoERQt6d
X3+QFxNWPxWW/BEoWkNjKfLHJgbYWQC5mgk7kK3u82DUV6fqGE95tiimhmSQwz8t
qfeS/nDVVCh2c2hARneHriD8LV7ve62gjfY0y6BfXJG8Jl0vg6kokgdGu9hABLDG
bj/BYjznC8qipN2I/X7Rn5eEr7RzxmMN3WigOFjsh4fUWNzcT6EP2CIphnmifPFx
gIwj7SPuWmB8itN2MV4EouF+MkEB9LSPQGeDxYn+hTnfucbVjlH9QVJ5vQDGyGkr
ZbmMB7HdriDj1CRWO22y2sxGHkKjRTb/uzkfy0XDNCJpw1SswaVcm9lep+aVq1zc
ocKGS8PTbFiSKGLHt+ythKqTcwWhFjStXgGxPVntw/aBzlfIfIUlA8bsteWAH6Ic
Go27fA6u0bBp+afq6gYbkTtGSY/leaFi1xfwX0G5uAT/AAVdObc=
=2h8G
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to get the tag name from within a taglib class ?

Rony G. Flatscher (Apache)
Hi Chris,

On 11.09.2020 19:31, Christopher Schultz wrote:
> > P.S.: If possible I would like to write a single tagclass, but use
> > it for two or more different tags, as the implementation would
> > share quite a lot of code. Besides, it might be helpful for
> > debugging.
>
> It seems like this is the wrong approach, unless you just want to use
> this for something like logging.

Not logging, but support for scripting languages.

> I you want the tags to behave differently, then you should have
> separate tag implementations.

In this case the only difference would be that in one case ("script", "scriptlet") a return value
gets ignored, in the other case ("expression") the return value is used to print its string value to
the JSPWriter ("out").

> Feel free to build a base class with the shared code and then implement the differences in subclasses.
Yes, if there is no easy way to learn about the tag that the tagclass got invoked for, which seems
to be the case here.
> It's been a loooong time since I wrote a custom tag library but I do
> remember that the whole process was very painful and despite the
> flexibility allowed by the API, lots of common things (like getting
> the tag name!) were either awkward or impossible.

Well, it is not really "painful", just a lot of power, many roads that could be chosen and
flexibility at hand which at times is overwhelming for a newbie in this corner, hence my brief
question. :)

> IMO, the JSP effort was a stepping-stone on a path to better
> technologies like Velocity, FreeMarker, and others. If I were king,
> JSP would just go away. Just my POV of course, you are welcome to fall
> in love with JSP. :)

This assumes that the JSPs get written in Java only and one is ready to take on the learning curves
for additional frameworks.

In this case the aim is to allow scripting languages either via Apache BSF [1] (like Groovy
implemented in Java, ooRexx implemented in C++) or via the Java scripting framework [2] ("JSR-223"
[3], "javax.script") to be used for writing JSPs. This may not be attractive/interesting for
Java-savvy programmers who would create their server pages in Java, but it is *very* attractive,
even important for non-Java programmers who know one of the supported scripting languages.

In the end it should be easy to use Tomcat as the (Java-implemented) web server for web applications
that get implemented in other programming languages. Very much like the Apache web server [4] which
is written in C and allows other programming languages to exploit it.

---rony

P.S.: Maybe I should come up with a brief posting that tries to explain why it is important to
support non-Java programming languages in JSPs by default (and if interestesting to the Tomcat
community I can offer the code under AL, once it is done). Not sure whether all in the Tomcat
community know that Apache BSF ("Bean Scripting Framework") was created originally to allow
scripting languages to be used in JSPs! Hence I was very surprised to learn that scripting languages
are not supported in Tomcat by default.

[1] "Apache Bean Scripting Framework (BSF)": <https://commons.apache.org/proper/commons-bsf/>

[2] Java package "javax.script" ("JSR-223"):
<https://docs.oracle.com/javase/8/docs/api/javax/script/package-summary.html>

[3] "JSR-223", the "Java specification request 223":
<https://en.wikipedia.org/wiki/Scripting_for_the_Java_Platform> or
<https://jcp.org/en/jsr/detail?id=223>

[4] "Apache Web Server", "Apache HTTP Server Project": <https://httpd.apache.org/>





Reply | Threaded
Open this post in threaded view
|

RE: How to get the tag name from within a taglib class ?

Berneburg, Cris J. - US-2
In reply to this post by Christopher Schultz-2
Rony

RF> If possible I would like to write a single tagclass, but use it
RF> for two or more different tags, as the implementation would share
RF> quite a lot of code. Besides, it might be helpful for debugging.

CS> Feel free to build a base class with the shared code and then implement
CS> the differences in subclasses.

Sure, implementing a sub-class of TagSupport that acts as a custom tag super-class works fine.

RF> I would have a need to find out the tag name
RF> that caused the tagclass to run.
RF>
RF> Is this possible? If so, how would one be able to get at
RF> that tag name (any brief hints would suffice) ?

Not sure exactly what you mean...

Here's an example:

- CustomTagImplementation.java contains:
public class CustomTagImplementation extends TagSupport

- tags.tld contains:
<tag>
    <name>CustomTag1</name>
    <tag-class>tags.CustomTagImplementation</tag-class>
</tag>
<tag>
    <name>CustomTag2</name>
    <tag-class>tags.CustomTagImplementation</tag-class>
</tag>

- page.jsp contains:
<td>
    <tags:CustomTag1 />
</td>

Are either of these what you mean?

A. Get surrounding tag "<td>" - TagSupport/CustomTagImplementation/getParent(), for calculating something like an xpath?

B. Get tag definition name "CustomTag1" - Ouch.  Sorry, no help there.  How about an intellectual exercise to kill some time?  :-)

Suppose you had a map of classes and associated tag names from either parsing tags.tld directly or exposing whatever structure holds classes instantiated from it.  You might still have a lookup problem due to a one-to-many relationship.  Using CustomTagImplementation/getClassName() as the map lookup key would have 2 theoretical answers, both "CustomTag1" and "CustomTag2".

By the time the page code is executed, the JSP has aleady been compiled.  Looking at pre-compiled org/apache/jsp/page_jsp.java, each call to <tags:CustomTag1 /> gets its own _jspx_meth_* method.  There is a comment in each method, "//  tags:CustomTag1".  Seems kinda messy in there, and potentially fragile to depend on the pre-compiler output format.

It's too bad TagSupport is not auto-magically fed as a parameter the name of tag definition in the page that "called" it.  All in the engine, no changes to JSP pages.

While I have not used it myself, have you looked at SimpleTagSupport to see if it has something useful?

--
Cris Berneburg
CACI Senior Software Engineer


________________________________

This electronic message contains information from CACI International Inc or subsidiary companies, which may be company sensitive, proprietary, privileged or otherwise protected from disclosure. The information is intended to be used solely by the recipient(s) named above. If you are not an intended recipient, be aware that any review, disclosure, copying, distribution or use of this transmission or its contents is prohibited. If you have received this transmission in error, please notify the sender immediately.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]