Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP Custom Tag Library vs JSP2 Tag Files

Tags:

jsp

jsp-tags

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?

like image 223
Vinayak Bevinakatti Avatar asked Oct 23 '08 05:10

Vinayak Bevinakatti


2 Answers

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

like image 184
machineghost Avatar answered Nov 10 '22 01:11

machineghost


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.
like image 42
Vinayak Bevinakatti Avatar answered Nov 10 '22 03:11

Vinayak Bevinakatti