- 7.1 Tag Library Components
- 7.2 Example: Simple Prime Tag
- 7.3 Assigning Attributes to Tags
- 7.4 Example: Prime Tag with Variable Length
- 7.5 Including Tag Body in the Tag Output
- 7.6 Example: Heading Tag
- 7.7 Example: Debug Tag
- 7.8 Creating Tag Files
- 7.9 Example: Simple Prime Tag Using Tag Files
- 7.10 Example: Prime Tag with Variable Length Using Tag Files
- 7.11 Example: Heading Tag Using Tag Files
7.5 Including Tag Body in the Tag Output
Up to this point, all of the custom tags you have seen did not allow a body and thus were always used as standalone tags of the following form:
<prefix:tagname/> <prefix:tagname></prefix:tagname>
Note that the second tag shown does not have any space between the opening and closing tags. The fact that these tags were not allowed to include a body was a direct result of supplying the element body-content with the value of empty.
In this section, we see how to define tags that use their body content and are thus written in the following matter:
<prefix:tagname>scriptless JSP content</prefix:tagname>
Tag Bodies: Tag Handler Class
Supporting tag bodies does not introduce any structural changes to the tag handler class. You still need to include setter methods for any attributes you are planning to declare and use. You still need to override the doTag method. To output the body content of the tag, inside the doTag method you need to acquire the JspFragment instance representing the body of the tag by calling the getJspBody method, then using its invoke method passing it null as its argument. Usually, this is done in a single step as follows:
getJspBody().invoke(null);
The container processes the JSP content found in the body of the tag just like any other JSP page content. If the invoke method is passed null as its argument, the resulting output of that JSP content is passed verbatim to the client. Therefore, the doTag method has no way of accessing the tag body output. All it can do is pass it along. We show how to access and modify the output of the tag body content before it's sent to the client in Section 8.1 (Manipulating Tag Body). It's important to stress, however, that it is the output resulting from the execution of the JSP code in the tag body, not the JSP code itself, that is passed to the client.
In practice, you almost always output something before or after outputting the tag body as follows:
JspWriter out = getJspContext().getOut(); out.print("..."); getJspBody().invoke(null); out.print("...");
Note that because sending the JSP content of the tag body boils down to a simple method invocation, it is very easy to create a tag that conditionally sends the JSP content to the client by surrounding the method call with an if statement. We show an example of this in Section 7.7 (Example: Debug Tag). It is also trivial to output the tag body content several times, as the method call can be placed inside a for loop and invoked many times. We show an example of this in Section 8.4 (Example: Simple Looping Tag).
Tag Bodies: Tag Library Descriptor
The change to the TLD is trivial. Instead of the value of empty for the required body-content element, we need to provide the value of scriptless.
Tag Bodies: JSP File
There are no changes to the JSP file. You still need to declare and assign a prefix to the TLD through the taglib directive. However, now we can use our tags with nonempty bodies.
Remember, however, that the body-content was declared as scriptless, and that scriptless means we are allowed to place JSP content into the body of the tag, but are not allowed to place JSP scriptlets there. So, the following is a legal usage of the tag:
<prefix:tagname> some content with ${bean.property} </prefix:tagname>
The following would be illegal:
<prefix:tagname> some content with <%= bean.property %> </prefix:tagname>