You use this procedure only if the tag file is placed in a JAR file in the /WEB-INF/libdirectory. In the alternative case, when the tag file is placed directly in the /WEB-INF/tags directory, using this procedure is unnecessary. This is one of the advantages of tag files. In the latter case, the Web Container provides an implicit TLD and instantiates the tags for you. You can use the tags in a JSP page when you import them with the tagdir attribute of the taglib directive.
You add tags to the TLD using the <tag-file> element.
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 described in the TLD schema in the JSP 2.1 specification. The mandatory elements in a TLD are the taglib and its child elements tlib-version and short-name.
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 have a tag file packaged in a JAR file in the /WEB-INF/lib directory. The path to the tag file inside the JAR is /META-INF/tags/myTag.tag.
<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/HelloSimpleTagFile</uri> <tag-file> <name>myTag</name> <path>/META-INF/tags/myTag.tag</path> </tag-file> </taglib> |
You create a JSP page, example.jsp, to use the tag and import the TLD. We can use one of the following ways:
● If you explicitly describe the tag files in a TLD, you use the uri attribute of the taglib directive to import the tags.
<%@ taglib prefix="my" uri=" http://com.sap.testExample/HelloSimpleTagFile " %> |
● If you do not describe the tags in a TLD, you use the tagdir attribute of the taglib directive to import the tags.
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> |
To invoke the tag and specify its attributes:
<my:myTag bgcolor="#FF0000" title="myTable"/> |