background image

Types of Tags

<< Configuring the Example | Dynamic Attributes >>
<< Configuring the Example | Dynamic Attributes >>

Types of Tags

Types of Tags
Simple tags are invoked using XML syntax. They have a start tag and an end tag, and possibly a
body:
<tt:tag>
body</tt:tag>
A custom tag with no body is expressed as follows:
<tt:tag /> or <tt:tag></tt:tag>
Tags with Attributes
A simple tag can have attributes. Attributes customize the behavior of a custom tag just as
parameters customize the behavior of a method. There are three types of attributes:
Simple attributes
Fragment attributes
Dynamic attributes
Simple Attributes
Simple attributes are evaluated by the container before being passed to the tag handler. Simple
attributes are listed in the start tag and have the syntax attr="value". You can set a simple
attribute value from a String constant, or an expression language (EL) expression, or by using a
jsp:attribute
element (see
"jsp:attribute Element" on page 231
). The conversion process
between the constants and expressions and attribute types follows the rules described for
JavaBeans component properties in
"Setting JavaBeans Component Properties" on page 169
.
The Duke's Bookstore page
tut-install/javaeetutorial5/examples/web/bookstore3/web/bookcatalog.jsp calls the
catalog
tag, which has two attributes. The first attribute, a reference to a book database object,
is set by an EL expression. The second attribute, which sets the color of the rows in a table that
represents the bookstore catalog, is set with a String constant.
<sc:catalog bookDB =
"${bookDB}" color="#cccccc">
Fragment Attributes
A JSP fragment is a portion of JSP code passed to a tag handler that can be invoked as many
times as needed. You can think of a fragment as a template that is used by a tag handler to
produce customized content. Thus, unlike a simple attribute which is evaluated by the
container, a fragment attribute is evaluated by a tag handler during tag invocation.
Types of Tags
Chapter 8 · Custom Tags in JSP Pages
229