Using Custom Tags
Custom tags are user-defined JSP language elements that encapsulate recurring tasks. Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags.
Custom tags have the syntax
or
where
prefixdistinguishes tags for a library,tagis the tag identifier, andattr1 ... attrNare attributes that modify the behavior of the tag.To use a custom tag in a JSP page, you must
See Chapter 7 for detailed information on the different types of tags and how to implement tags.
Declaring Tag Libraries
To declare that a JSP page will use tags defined in a tag library, you include a
taglibdirective in the page before any custom tag from that tag library is used. If you forget to include thetaglibdirective for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as static data and will simply insert the text of the custom tag call into the response.The
prefixattribute defines the prefix that distinguishes tags defined by a given tag library from those provided by other tag libraries.If the tag library is defined with tag files (see Encapsulating Reusable Content Using Tag Files, page 204), you supply the
tagdirattribute to identify the location of the files. The value of the attribute must start with/WEB-INF/tags/. A translation error will occur if the value points to a directory that doesn't exist or if it is used in conjunction with theuriattribute.The
uriattribute refers to a URI that uniquely identifies the tag library descriptor (TLD), a document that describes the tag library (see Tag Library Descriptors, page 220).Tag library descriptor file names must have the extension
.tld. TLD files are stored in theWEB-INFdirectory or subdirectory of the WAR file or in theMETA-INF/directory or subdirectory of a tag library packaged in a JAR. You can reference a TLD directly or indirectly.The following
taglibdirective directly references a TLD file name:This
taglibdirective uses a short logical name to indirectly reference the TLD:The
iteratorexample defines and uses a simple iteration tag. The JSP pages use a logical name to reference the TLD. To build the example, follow these steps:To deploy the example, run
antdeploy.To run the
iteratorapplication, open the URLhttp://localhost:8080/iteratorin a browser.To learn how to configure the example, refer to the deployment descriptor, which includes the following configurations:
- A
display-nameelement that specifies the name that tools use to identify the application.- Nested inside a
jsp-configelement is ataglibelement, which provides information on a tag library used by the pages of the application. Inside thetaglibelement are thetaglib-urielement and thetaglib-locationelement. Thetaglib-urielement identifies the logical name of the tag library. Thetaglib-locationelement gives the absolute location or the absolute URI of the tag library.The absolute URIs for the JSTL library are as follows:
When you reference a tag library with an absolute URI that exactly matches the URI declared in the
taglibelement of the TLD (see Tag Library Descriptors, page 220), you do not have to add thetaglibelement toweb.xml; the JSP container automatically locates the TLD inside the JSTL library implementation.Including the Tag Library Implementation
In addition to declaring the tag library, you also must make the tag library implementation available to the web application. There are several ways to do this. Tag library implementations can be included in a WAR in an unpacked format: Tag files are packaged in the
/WEB-INF/tag/directory, and tag handler classes are packaged in the/WEB-INF/classes/directory of the WAR. Tag libraries already packaged into a JAR file are included in the/WEB-INF/lib/directory of the WAR. Finally, an application server can load a tag library into all the web applications running on the server. For example, in the Application Server, the JSTL TLDs and libraries are distributed in the archiveappserv-jstl.jarin<JavaEE_HOME>/lib/. This library is automatically loaded into the classpath of all web applications running on the Application Server so you don't need to add it to your web application.The
iteratortag library is implemented with tag handlers. Therefore, its implementation classes are packaged in the/WEB-INF/classes/directory. Click OK.