background image

Tag Handler Examples

<< Private Objects | Tag Handler >>
<< Private Objects | Tag Handler >>

Tag Handler Examples

<tt:param value=
"${requestScope.custNumber}" />
</tt:query>
<tt:connection ... >
<tt:query cid=
"balances">
SELECT account, balance FROM acct_table
where customer_number = ?
<tt:param value=
"${requestScope.custNumber}" />
</tt:query>
</tt:connection>
The TLD for the tag handler uses the following declaration to indicate that the connectionId
attribute is optional:
<tag>
...
<attribute>
<name>connectionId</name>
<required>false</required>
</attribute>
</tag>
Tag Handler Examples
The simple tags described in this section demonstrate solutions to two recurring problems in
developing JSP applications: minimizing the amount of Java programming in JSP pages and
ensuring a common look and feel across applications. In doing so, they illustrate many of the
styles of tags discussed in the first part of the chapter.
An Iteration Tag
Constructing page content that is dependent on dynamically generated data often requires the
use of flow control scripting statements. By moving the flow control logic to tag handlers, flow
control tags reduce the amount of scripting needed in JSP pages. Iteration is a very common
flow control function and is easily handled by a custom tag.
The discussion on using tag libraries in
Chapter 5, "JavaServer Pages Technology"
introduced a
tag library containing an iterator tag. The tag retrieves objects from a collection stored in a
JavaBeans component and assigns them to an EL variable. The body of the tag retrieves
information from the variable. As long as elements remain in the collection, the iterator tag
causes the body to be reevaluated. The tag in this example is simplified to make it easy to
demonstrate how to program a custom tag. web applications requiring such functionality
should use the JSTL forEach tag, which is discussed in
"Iterator Tags" on page 208
.
Programming Simple Tag Handlers
Chapter 8 · Custom Tags in JSP Pages
265