File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Custom Tag inhertance. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Custom Tag inhertance." Watch "Custom Tag inhertance." New topic

Custom Tag inhertance.

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
I have a JSP custom tag "A" defined in my .tld. It has 3 required attributes. It has its ATagHAndler that inherits SimpleTagHandler.
I need a new tag "B" that does excatly the same thing as the above "A" but in a little different way. Also the attributes that are present in "A", i copied them to "B" in the .tld file (Code duplication)). Further the I created a BTagHandler extends ATagHandler, and overrided the few methods that actually changes across these two handlers.and rest functionality is defined in "ATagHandler. On java side its cool, cause lot of code duplication is avoided due to inheritance.

But in the .tld file that has the defination of Tag A and TAg B, there are duplication of the attributes definition. Is there a way to avoid this duplication.
If not,
Is there a completely different approach for my problem. (Both tags do excatly same things, but a slight change, So i used inheritance)


Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
If both tags do exactly the same thing, I would only use one tag.

It sounds like your TagHandler has a lot of code logic in it, because usually you would only override doTag() or init().

So, i would suggest to encapsulate all the other business methods away into separate Helper classes, and then - since there is only a slight difference between A and B, determine in your tag handler how to process the tag.

JDBCSupport - An easy to use, light-weight JDBC framework -
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
If both tags do exactly the same thing, I would only use one tag. --> They do almost the same thing, the place where they differ hence i have two tagHandlers A, B.

A extends SimpleTagSupport.

B extends A
overrides the methods, the makes sense for B.

With this inheritance, lot of duplication is hidden.
My question was in the .tld file there are two definitions for A, B.
A has 3 attributes.
B has 4 attributes. only one extra that differs.
There is this duplication of attribute definition.
Cant we declare B, like all the attributes of A and then my extra attribute.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

No. The tag declarations in the TLD file cannot be hierarchical. You'll just need to live with duplication if you keep them as separate tags.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Custom Tag inhertance.
It's not a secret anymore!