Custom tags can do more than output data controlled by parameters. A custom tag can have a body, which it can control in arbitrary ways. Recall a similar tag, jsp:useBean, which renders its body only when the bean it is accessing is created. Listing 4.2 shows such a custom tag that can be used to display its body, hide it, or even reverse it. The result is shown in Figure 4.1.
Figure 4.1. The result of a custom tag.
Listing 4.2 A custom tag with a body
<%@ taglib prefix="awl" uri="http://jspbook.awl.com/samples" %> <awl:maybeShow show="no"> You can't see me! </awl:maybeShow><br> <awl:maybeShow show="yes"> The time is: <awl:date format="hh:mm:ss MM/dd/yy"/> </awl:maybeShow><br> <awl:maybeShow show="reverse"> The time is: <awl:date format="hh:mm:ss MM/dd/yy"/> </awl:maybeShow><br>
This example loads the same tag library used in Listing 4.1 and again specifies that it will be using the awl prefix to access the tags. The tag used this time is called awl:maybeShow, and it has a parameter, show, that controls what the tag should do with its body. This parameter may be set to no, in which case the body is hidden from the page; yes, in which case the body is displayed; or reverse, in which case the body is shown backward.
Note that the body of the awl:maybeShow tag may include anything, including other JSP tags. This was also true of the jsp:useBean tag and in fact is true of any custom tag that has been properly programmed. This property is described by saying that JSP tags can be nested. From here on, it will simply be assumed, unless otherwise noted, that the body of any tag can contain any other tag.