Thursday, May 20, 2004

Definitive checklist on how to include a taglib


acquired from: http://www.mobilefish.com/developer/taglibs.html

Sorry, haven't got time to figure out how to escape all the angle brackets... look in the HTML source if it won't display properly!

How to use a (Custom) Tag Library in an application

Information:
none

Operating system used:
Windows XP Home Edition Version 5.1 SP 1

Software prerequisites:
none

Procedure:
Copy the library�s JAR file {library}.jar to your web application /WEB-INF/lib directory.
Copy the Tag Library Descriptor {library}.tld file to your web application /WEB-INF directory.
Modify your web application deployment descriptor (/WEB-INF/web.xml) file to map the Tag Library TLD URI to its location. For example:


/{library}
/WEB-INF/{library}.tld


or


http://jakarta.apache.org/taglibs/{library}
/WEB-INF/{library}.tld



To use the Tag Library in a JSP page, add the taglib directive <%@ taglib ... %> at the top of the JSP page. This directive identifies the URI of the library you want to use (which must match the value you specified in the element in the web application deployment descriptor), and the tagname prefix you will use within this page to identify tags from this library. The tag prefix is an arbitrary choosen name but keep it as simple as possible.

<%@taglib uri="/{library}" prefix="x"%>

or

<%@ taglib uri="http://jakarta.apache.org/taglibs/{library}" prefix="x" %>


To use one of the tags from this library, you would simply include it, prefixed as you described in the taglib directive, and including attribute names and values as described in the documentation for this tag library. For example, if the library you are using has a tag named magic, and you selected the "x" prefix as described above, you would might include this custom tag:



or, if this tag required attributes and perhaps some body text:

"x:magic id="beanName" name="xyz">
... Some body text and/or nested tags ...



See the documentation provided with each custom tag library for detailed descriptions of the available tags within that library.

Labels: