The taglib, tag, include, attribute and the variable Directive in JSP

By: Sathya Narayana Viewed: 153245 times    

The taglib Directive

A tag library is a collection of tags used to extend a JSP container functional model. The taglib directive defines a tag library namespace for the page, mapping the uniform resource indicator (URI) of the tag library descriptor to a prefix that can be used to reference tags from the library on this page.

<%@ taglib (uri="tagLibraryURI" | tagdir="tagDir") prefix="tagPrefix" %>

.
.
.

<tagPrefix:tagName attributeName="attributeValue" >
  JSP content
</tagPrefix:tagName>

<tagPrefix:tagName attributeName="attributeValue" />

You can assume that the tag library descriptor (TLD) defines a tagName element. tagdir indicates this prefix is for identifying tag extensions installed in the /WEB-INF/tags/ directory or a subdirectory. If a TLD is present in the specified directory, it's used. Otherwise, an implicit tag library descriptor, generated by the container, is used. A translation error must occur if the value doesn't start with /WEB-INF/tags/. A translation error must occur if the value doesn't point to a directory that exists. A translation error must occur if used in conjunction with the uri attribute.

The tag Directive

You can use most JSP directives in simple tag handler code files. Note that the page directive itself isn't used; instead, you use the tag directive, which may only be used in tag files. Here's the syntax:

<%@ tag tag_directive_attr_list %>

tag_directive_attr_list ::=
  { display-name="display-name" }
  { body-content="scriptless|tagdependent|empty" }
  { dynamic-attributes="name" }
  { small-icon="small-icon" }
  { large-icon="large-icon" }
  { description="description" }
  { example="example" }
  { language="scriptingLanguage" }
  { import="importList" }
  { pageEncoding="peinfo" }
  { isELIgnored="true|false" }

This is an example tag directive:

<%@ tag name="msg"
  display-name="Message"
  body-content="scriptless"
  dynamic-attributes="user"
  small-icon="/WEB-INF/small-icon.jpg"
  large-icon="/WEB-INF/large-icon.jpg"
  description="Simple usage of a tag directive"
%>

The include Directive

There are two include tags: the include directive and the jsp:include action.

The include directive includes a static file at translation time, adding any JSP in that file to this page for run-time processing:

<%@ include file="header.html" %>

The attribute Directive

The attribute directive is analogous to the <attribute> element in the TLD and allows you to declare custom action attributes. This is the syntax:

<%@ attribute attribute_directive_attr_list %>

attribute_directive_attr_list ::=
  name="attribute-name"
  { required="true|false" }
  { fragment="true|false" }
  { rtexprvalue="true|false" }
  { type="type" }
  { description="description" }

The variable Directive

The variable directive is analogous to the <variable> element in the TLD and allows you to define a variable exposed by the tag handler. This is the syntax:

<%@ variable variable_directive_attr_list %>

variable_directive_attr_list ::=
  ( name-given="output-name" |
    ( name-from-attribute="attr-name" alias="local-name"))
  { variable-class="output-type" }
  { declare="true|false" }
  { scope="AT_BEGIN|AT_END|NESTED" }
  { description="description" }

Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial