Chapter 9) Using Tag Libraries

9.1) For a custom tag library or a library of Tag Files, create the 'taglib' directive for a JSP page.

This objective was addressed briefly in objective 6.6. The taglib directive for a JSP page takes the format.

<%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>

The URI can take the form of either pointing directly to the tag library descriptor file or it can point to a URI within the deployment descriptor (WEB.XML) that then points to the tag library descriptor file. It is generally considered more flexible to use a uri that maps to a uri within the deployment descriptor.

The following taglib directive shows how the uri can point directly to the tag library descriptor file.

<%@ taglib uri="WEB-INF/taglib.tld" prefix="h" %>

Wrapping a taglib in a Jar

As was shown with the JSTL it is common to put taglibs in a jar file once the development process has finished. This gives the simplicity of being able to deliver the functionality of multiple taglibs in a single file rather than needing to copy multiple files to multiple directories. The following example shows how a TagLib called SimpleTagDemo can be wrapped in a Jar and made available to a JSP page. Don't worry about the actual code of the taglib, it is covered in more detail in section 10.4. The file SimpleTagDemo.java contains the following code.

/*
* @author Marcus Green
 * May 2006
 * A trivial example of
 * a simple tag
 */
package com.examulator;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class SimpleTagDemo extends SimpleTagSupport {
    public void doTag() throws JspException{
        PageContext context = (PageContext) getJspContext();
       JspWriter out = context.getOut();
        try {
            out.println("SimpleTagSupport demo");
        } catch (Exception e) {/* log the error */}        
    }
}

The file was created\copied to the directory

\webapp\WEB-INF\classes\com\examulator

And compiled from directory

\webapp\WEB-INF\classes

with the command line

javac com\examulator\SimpleTagDemo.java

The output file SimpleTagDemo.class was copied to

\webapp\META-INF\tags

A file called taglib.tld was created in the directory

\webapp\META-INF\

with the following text.

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>simpletagdemo</short-name>
  <uri>/WEB-INF/tlds/SimpleTagDemo</uri>
   <tag>
        <name>SimpleTagDemo</name>
        <tagclass>com.examulator.SimpleTagDemo</tagclass>
         <body-content>scriptless</body-content>
        <info>Demonstrates use of the SimpleTagSupport class</info>   
   </tag>   
</taglib>

The class file and its tld were wraped in a jar file with the following command executed from the webapp directory

jar -cvf SimpleTagDemo.jar META-INF\*

The resulting jar file can be deployed by copying it to the \webapp\WEB-INF\

The taglib can be directly access in the following manner in a jsp page.

<%@ taglib uri="WEB-INF/lib/SimpleTagDemo.jar" prefix="h" %>
<h:SimpleTagDemo/>
<h1>Here we go </h1>

It could also be deployed by adding an entry into the deployment descriptor that pointed to the jar file.

The Taglib prefix

The prefix attribute is mandatory and must be unique within the page, i.e. You could not have two taglib directives using the prefix h within the same page even if they each had a different URI.

Instead of the uri attribute the tagdir attribute can be used. If the tagdir attribute is used it must point to a directory starting with "/WEB-INF/tags/. Thus the following is a valid taglib directive that uses tagdir.

<%@ taglib tagdir="/WEB-INF/tags/taglib.tld" prefix="h" %>

URI reserved words

The following words are reserved and cannot be used as a prefix for a taglib jsp:, jspx:, java:, javax:, servlet:, sun:, and sunw


Other sources

The taglib directive according to Sun
http://java.sun.com/products/jsp/syntax/1.2/syntaxref1211.html

According to Mikalai Zaikin
http://java.boot.by/wcd-guide/ch09.html#c9s1

Last modified: Sunday, 20 September 2015, 07:21 PM