In this section, we will discuss about JSP "taglib" directive with a small example.
In this section, we will discuss about JSP "taglib" directive with a small example.In this section, we will discuss about JSP "taglib" directive with a small example. The JSP "taglib" directive is use to define tag library, which is the collection of tags and it also defines the prefix for the tags. To use custom tags in your JSP , you have to use it's syntax to import the classes which contain the definition of these tags.
SYNTAX:
<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> |
The following table describe the attribute of the taglib directive :
Attribute Name | Description |
uri | Locates the TLD file of a custom tag . |
prefix | Defines a prefix string to be used for distinguishing a custom tag instance |
A Tag library is of two kinds :-
The "prefix" attribute is a must for custom as well as standard tags in JSP. Example of standard tag library is JSTL or JSP Standard tag library.
EXAMPLE :
In this example , we are using "Standard" library(JSTL) library. First, you have to download ' jstl-1.2.jar ' from the below link:
http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadjstl12jar.htm
Second , paste it in Tomcat's "lib" folder. for example : 'C:\Program Files\Tomcat 6.0\lib '.
Final Step , run the JSP code given below in your web browser :-
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <p>Simple Example for C:out</p> Multiply 85 and 2::<c:out value="${85*2}" /> |
Output
Download JSTL support from the Jakarta Website then follow these steps:
common/lib
in your
Tomcat installation (although you won't need all the jar files for our
project). This makes the JSTL jar files available to any of your Web
applications. .tld
files to the WEB-INF
directory in your Web
application. web.xml
file and
add the following entries:
<taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> |
These entries let your Web application use the expression language (EL)
versions of the JSTL tag libraries. Position of these entries matters! If you're
not sure where to put them, the definitive guide to web.xml
options
and ordering is defined in the document type definition (DTD) at:
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd.
".jsp"
extension.