Given a scenario, write tag handler code to access the parent tag and an arbitrary tag ancestor.

An object created by the enclosing tag handler of a group of nested tags is available to all inner tag handlers. This form of object sharing has the advantage that it uses a private namespace for the objects, thus reducing the potential for naming conflicts. To access an object created by an enclosing tag, a tag handler must first obtain its enclosing tag with the static method TagSupport.findAncestorWithClass(from, class) or the TagSupport.getParent() method. The former method should be used when a specific nesting of tag handlers cannot be guaranteed. Once the ancestor has been retrieved, a tag handler can access any statically or dynamically created objects. Statically created objects are members of the parent. Private objects can also be created dynamically. Such objects can be stored in a tag handler with the setValue method and retrieved with the getValue method.

The following example illustrates a tag handler that supports both the named and private object approaches to sharing objects. In the example, the handler for a query tag checks whether an attribute named connectionId has been set. If the connection attribute has been 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 could 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 indicates that the connectionId attribute is optional with the following declaration:

<tag>
	...
	<attribute>
		<name>connectionId</name>
		<required>false</required>
	</attribute>
</tag>

					

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide