April 20, 2010

Custom Tags, TLD, Tag Files, El Functions, what is the difference?

If you are starting out with Java Custom Tag Development, you have probably come across the terms in the title of this article.

But if you are like me, it probably took you a while to wrap your head around all the terms, how the relate to each other, and most importantly what is the difference between them.

  • What is a custom tag?
  • What is a TLD file?
  • What is the difference between a TLD file and a Tag File?
  • What is an EL Function?

In this article I will try to explain some of these questions and terms.

There is another article on this blog which you may find useful.

Custom Tags

A Custom Tag is a generic term for code which simplifies the writing of Java code (eg. scriptlets, classes, etc).

A simple example of a custom tag is: <myTagLibrary:doSomething/>

Custom Tags - JSTL (Java Standard Tag Library)

JSTL is a collection of standard tags which give you common functionality, that you otherwise would have to write yourself as scriptlets or expressions inside your JSP pages

For example:

Instead of putting this scriptlet in your JSP page


<table>

<%
  String[] items = (String[]) request.getAttribute("movieList"); 
  String movie = null;
  for(int i = 0;i<items.length;i++){
    movie = items[i];  
%>

<%
  <tr>
     <td><%= movie %></td>
  </tr>    
%>

</table>

You can make it much simpler by using a JSTL tag


<table>
  <c:forEeach var="movie" items="${requestScope.movieList}">
  <tr>
    <td>${movie}</td>
  </tr>  
  </c:forEach>
</table>

Custom Tags - Writing your own

You can also write your own Custom Tags. See how

Custom El Functions

A custom El Function is a customized method that you can invoke using the JSP Expression Language.

A simple custom EL Function looks like this: ${myTagLibrary:randomColor()}

How do I create a custom el function?

TLD Files

A TLD file (*.tld), or Tag Library Descriptor file, is an XML file containing data which defines a Custom Tag OR an El Function

How do I create a TLD file?

Tag Files

A Tag File (*.tag), is a way for non-Java developers to build custom tags without writing a Java class (eg. a Custom Tag Handler class).

A *.tag file can be any valid *.jsp file. So you can just rename a jsp file to *.tag, but there are certain rules about where in your application's directory structure you have to place a *.tag file.

A Tag File is usually used when you need simpler functionality in your custom tags, for complex things a Custom Tag Handler class is better.

How do I create a simple Tag File?

To learn more about Tag Files also try these articles:

Easy Custom Tags with Tag Files, Part 1 and Oracle article on Tag Files

Questions

What is the difference between a TLD File and a Tag File?

A TLD File is an XML file which declares the functionality of custom tags or EL Functions, while a Tag File (which is actually a renamed *.JSP file), allows you to declare Custom Tags without writing a Custom Tag Handler (eg. a Java class).