Can anybody explain the idea behind JSP custom tag libraries and the JSP 2 tag files?
Are they just different ways to do the same thing?
How do they compare? What are their pros and cons, and which is better?
I <3 tag files, but that lead developer of JSTL is smoking crack if they really said that. You CANNOT re-write all tag library tags as tag file tags, for one very important reason: tag files can't do:
return EVAL_BODY_INCLUDE;
In other words, tag files only have three options for their body-content:
empty: no inner content, ie. <someTag/>
scriptless: no JSP inner content, ie. <someTag><p>hello world</p></someTag> is ok, but not <someTag><p><%= helloWorld.toString() %></p></someTag>
tagdependent: you can have JSP inner content, but it won't be processed as such; instead you have to parse/render it however you see fit
But with the old style tag library tags, you can have: <body-content>JSP</body-content> (in the tld file) and then "return EVAL_BODY_INCLUDE;" from your "doStartTag". If you do this, all of your JSP directives will get parsed just as if they were a normal part of your page, and your tag simply wraps them with the appropriate content.
Personally, my rule of thumb is: use tag files whenever you can, ie. whenever you don't need JSP directives to work inside the tag, because they are a million times cleaner, easier for a non-programmer to work with, don't require a tld (well, if you keep them in a seperate namespace from your tag library tags).
But if you want JSP content inside your tag, your only option is tag library tags. Hopefully, someday, the JSP people will release a way to do JSP directive processing inside a tag file tag, and then we really can abandon the old class-based tags, but until then please don't try to do all tags with tag files, as you'll quickly be reduced to making custom tags for every last piece of logic (since that's the only way to do logic without using JSP directives).
Problems Developing Custom Tags
Traditional custom tags require Java programming skills.
All but the simplest custom tags are not easy to write.
The purpose of JSP, in contrast to servlets, is to use markup language to manage layout with embedded dynamic content.
Having to write complex Java code in custom tags that focus on markup language is going backwards.
We might want to use the JSP expression language or other custom tags when implementing a new custom tag.
The Solution JSP 2.0 Tag Files
How Do Tag Files Differ?
Written using JSP syntax.
Identified by either a .tag or .tagx suffix.
Intended to provide custom tag developers ease of development without loss of functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With