background image

The BodyTagSupport Class

<< CooperatingTags | Tags That Define Variables >>
<< CooperatingTags | Tags That Define Variables >>

The BodyTagSupport Class

set, the handler retrieves the connection object from the page context. Otherwise, the tag
handler first retrieves the tag handler for the enclosing tag and then retrieves the connection
object from that handler.
public class QueryTag extends BodyTagSupport {
public int doStartTag() throws JspException {
String cid = getConnectionId();
Connection connection;
if (cid != null) {
// there is a connection id, use it
connection =(Connection)pageContext.
getAttribute(cid);
} else {
ConnectionTag ancestorTag =
(ConnectionTag)findAncestorWithClass(this,
ConnectionTag.class);
if (ancestorTag == null) {
throw new JspTagException(
"A query without
a connection attribute must be nested
within a connection tag.
");
}
connection = ancestorTag.getConnection();
...
}
}
}
The query tag implemented by this tag handler can be used in either of the following ways:
<tt:connection cid=
"con01" ... >
...
</tt:connection>
<tt:query id=
"balances" connectionId="con01">
SELECT account, balance FROM acct_table
where customer_number = ?
<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 use the following declaration to indicate that the connectionId
attribute is optional:
Programming Tags That Accept Scripting Elements
Chapter 9 · Scripting in JSP Pages
283