Chapter 7. Ajax Tags
Writing Ajaxian code over and over can be tedious and error-prone. Although many libraries with a lot of reusable code are available, you may develop some custom features that you want to use repeatedly throughout your applications. In that case, you can encapsulate the functionality in a JSP tag for easy reuse. Using tag libraries will save you time and enhance the maintainability of your code. If you fix a bug in a tag library, you can easily propagate the change to all of your code: instead of rewriting the code in every application, merely drop your new library into the deploy directory.
Creating a Tag Library
In the two previous chapters, we developed code that enabled us to look up city and state information based on a user-entered zip code. Now we’ll explore how to add this feature to a tag library, encapsulating the details of the Ajax code. To start, we must define our tag. Tags are defined in Tag Library Definition (TLD) files. The names of these files end with the extension .tld; in this example, the TLD file will be called ajax-oreilly.tld.
Writing a TLD
A TLD is written in XML and describes the attributes of each tag in the tag library. The parent tag is <taglib>
; within that tag, you need a <tag>
tag for each type of tag that the TLD defines. Our library will have only a single tag, but most libraries define many tags.
Each <tag>
definition must have a <name>
tag, which specifies the tag’s name, and a <tagclass>
tag. The <tagclass>
tag specifies ...
Get Ajax on Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.