tags:

views:

1601

answers:

2

Can anybody gimme the idea about the JSP custom tag library and the JSP 2 Tag files..

Are this two are alternatives to use....???

If there is any comparision(or merits and demerits) among this please do specify..

And which one of this is better to work with..

I m waiting for the kind suggestions..

Regards, Vinayak

+4  A: 

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

  • Tag files are one of the important new concepts introduced with JSP 2.0.
  • Tag files permit easier and more rapid development of custom tags.
  • Tag files are developed using normal JSP syntax,including scripting elements, and used just like any other custom tag.
  • Each custom tag is a separate tag file.

How Do Tag Files Differ?

Written using JSP syntax.

  • Not all JSP directives are permitted in a tag file.
  • Supported by new tag file specific directives, actions and implicit objects.

Identified by either a .tag or .tagx suffix.

Intended to provide custom tag developers ease of development without loss of functionality.

  • One of the lead developers of JSTL has commented thatif she had the time, she would rewrite all of JSTL using tag files.
Vinayak.B
+4  A: 

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).

machineghost