To use the tag handler in a JSP, you create a TLD and describe the tag using the <tag> element. This procedure is applicable for both classic and simple tag handlers.
More information about creating TLD Files: Developing TLD Files.
Starting with JSP 2.1, the TLDs you create must exactly follow the order of the elements as described in the TLD schema in the JSP 2.1 specification. The mandatory elements in a TLD are the taglib and short-name elements. Depending on the component you wish to use in your Web application, you also use a tag, tag-file, or function element respectively. We recommend that you also include the uri element. The order of elements defined in the schema is the following:
description
display-name
icon
tlib-version
short-name
uri
validator
listener
tag
tag-file
function
We describe a tag handler in a TLD file, myTLD.tld, placed directly in the /WEB-INF directory, using the following source code:
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd" version="2.1">
<tlib-version>1.0</tlib-version> <short-name>Hello</short-name> <uri>http://com.sap.testExample/Hello</uri>
<tag> <name>HelloWorld</name> <tag-class>com.sap.demo.exampletags.HelloWorldTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>bgcolor</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>title</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> |
The value of the <tag-class> tag must be the fully-qualified name of the tag handler class.
In our Web application, we package the tag handler classes under /WEB-INF/classes.
The recommended way to describe the path to the TLD file is by using the web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> ... <jsp-config> <taglib> <taglib-uri>myTaglib</taglib-uri> <taglib-location>/WEB-INF/myTLD.tld</taglib-location> </taglib> </jsp-config> </web-app> |
In the JSP, we use the mapping from the web.xml in the following way:
<%@ taglib prefix="my" uri="myTaglib" %> |
Alternatively, we can omit the TLD definition in the web.xml, and use directly in the JSP the uri defined in the TLD, which must be unique throughout the application.
<%@ taglib prefix="my" uri="http://com.sap.testExample/Hello" %> |
We invoke the tag in the same way with tag files and tag handlers.
<my:myTag bgcolor="#FF0000" title="myTable">Some data</my:myTag> |