I’ve written my fair share of JSP 1.0 tags, the hell of deciding whether to extend TagSupport or extend BodyTagSupport and implement BodyTag, writing tons of StringBuffer append statements left alot to be desired. Almost all of the tags that I wrote wrapped up some piece of presentation logic with a bunch of HTML, which meant alot of header.append(“html content”) type statements. Luckily, it seems that there were other people who had bad experiences with the JSP 1.2 specification (or at least they understood that it needed improvement) which lead to the JSP 2.0 specification. Among other things, JSP 2.0 includes the ability to create JSP tags *using* JSP syntax, which means that front-end/GUI developers that don’t know Java can now easily create reusaeable tags without having to write and compile a Java class. It’s brain dead simple. Create a file in /WEB-INF/tags/ OR /META-INF/tags/ OR create a jar of all your tag files and place them in /WEB-INF/lib/, save it with a .tag or a .tagx extension, enter some content (ie: “Hello World!”) and then you can invoke the tag in a JSP by importing the taglib:
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
and then the tag itself:
<tags:helloWorld/>
The prefix is determined by the taglib directive (in this case the prefix is ‘tags’) and the name of the tag is determined by the file name of the tag that you saved.
Using attributes is just as easy. At the beginning of the tag file you can add as many attribute directives as you want:
<%@ attribute name="title" %>
and then write their values to the screen:
<strong>${title}</strong>
I’m just scratching the surface though. There are 2 great articles on java.net to get you started if you want to learn more:
Easy Custom Tags with Tag Files, Part 1
Easy Custom Tags with Tag Files, Part 2
You can also read the JSP 2.0 specification (JSR-152).
As an aside, one of the things I always wanted to be able to do with ColdFusion was to deploy the custom tags I wrote as part of the application, in a non web-accessible directory without having modify the custom tag path through ColdFusion administrator. For instance, if you deploy a ColdFusion application to a shared host and you want to use your own custom tags, you have to either store the custom tags in the same directory as the script that you call them from (which does not help much at all), you an use cfmodule, which is a kludge, or you can ask the ISP/host to either add the directory where your tags live to the custom tag path or to put your custom tags into the existing custom tag path, neither of which is an attractive (or secure!) option. I haven’t used ColdFusion for awhile… can you do that now?