How can a JSP Tag discover its own name?

Discussion in 'Java' started by robin, Aug 16, 2007.

  1. robin

    robin Guest

    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
    robin, Aug 16, 2007
    #1
    1. Advertising

  2. robin

    Lew Guest

    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
    Lew, Aug 16, 2007
    #2
    1. Advertising

  3. robin

    robin Guest

    On Aug 16, 3:07 pm, Lew <> 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
    robin, Aug 20, 2007
    #3
  4. robin

    Guest

    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 <> 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
    , Aug 20, 2007
    #4
  5. robin

    robin Guest

    On Aug 20, 11:00 am, 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 <> 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.
    robin, Aug 20, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    779
    John C. Bollinger
    Jan 27, 2006
  2. Saverio M.
    Replies:
    0
    Views:
    509
    Saverio M.
    Jul 3, 2006
  3. Replies:
    8
    Views:
    308
    Fredrik Lundh
    Nov 20, 2005
  4. thunk
    Replies:
    1
    Views:
    303
    thunk
    Mar 30, 2010
  5. Aaron Gray

    Can a JavaScript object get its own name ?

    Aaron Gray, Jan 5, 2007, in forum: Javascript
    Replies:
    2
    Views:
    129
    Aaron Gray
    Jan 5, 2007
Loading...

Share This Page