background image

Tags with Bodies

<< Deferred Value and Method | Communication between Tags >>
<< Deferred Value and Method | Communication between Tags >>

Tags with Bodies

The body of jsp:attribute is restricted according to the type of attribute being specified:
For simple attributes that accept an EL expression, the body can be any JSP content.
For simple attributes that do not accept an EL expression, the body can contain only static
text.
For fragment attributes, the body must not contain any scripting elements (see
Chapter 9,
"Scripting in JSP Pages"
).
Tags with Bodies
A simple tag can contain custom and core tags, HTML text, and tag-dependent body content
between the start tag and the end tag.
In the following example, the Duke's Bookstore application page
tut-install/javaeetutorial5/examples/web/bookstore3/web/bookshowcart.jsp uses the
JSTL c:if tag to print the body if the request contains a parameter named Clear:
<c:if test=
"${param.Clear}">
<font color=
"#ff0000" size="+2"><strong>
You just cleared your shopping cart!
</strong><br>&nbsp;<br></font>
</c:if>
jsp:body
Element
You can also explicitly specify the body of a simple tag by using the jsp:body element. If one or
more attributes are specified with the jsp:attribute element, then jsp:body is the only way to
specify the body of the tag. If one or more jsp:attribute elements appear in the body of a tag
invocation but you don't include a jsp:body element, the tag has an empty body.
Tags That Define Variables
A simple tag can define an EL variable that can be used within the calling page. In the following
example, the iterator tag sets the value of the EL variable departmentName as it iterates
through a collection of department names.
<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>
Types of Tags
The Java EE 5 Tutorial · September 2007
232