background image

Tag Handler

<< Tag Handler Examples | A Template Tag Library >>
<< Tag Handler Examples | A Template Tag Library >>

Tag Handler

JSP Page
The index.jsp page invokes the iterator tag to iterate through a collection of department
names. Each item in the collection is assigned to the departmentName variable.
<%@ taglib uri=
"/tlt" prefix="tlt" %>
<html>
<head>
<title>Departments</title>
</head>
<body bgcolor=
"white">
<jsp:useBean id=
"myorg" class="myorg.Organization"/>
<table border=2 cellspacing=3 cellpadding=3>
<tr>
<td><b>Departments</b></td>
</tr>
<tlt:iterator var=
"departmentName" type="java.lang.String"
group=
"${myorg.departmentNames}">
<tr>
<td><a href=
"list.jsp?deptName=${departmentName}">
${departmentName}</a></td>
</tr>
</tlt:iterator>
</table>
</body>
</html>
Tag Handler
The collection is set in the tag handler by means of the group attribute. The tag handler retrieves
an element from the group and passes the element back to the page in the EL variable whose
name is determined by the var attribute. The variable is accessed in the calling page using the
JSP expression language. After the variable is set, the tag body is evaluated with the invoke
method.
public void doTag() throws JspException, IOException {
if (iterator == null)
return;
while (iterator.hasNext()) {
getJspContext().setAttribute(var, iterator.next());
getJspBody().invoke(null);
}
}
public void setVar(String var) {
this.var = var;
}
public void setGroup(Collection group) {
Programming Simple Tag Handlers
The Java EE 5 Tutorial · September 2007
266