[ Team LiB ] Previous Section Next Section

Conditionally Including the Body of a Custom Tag

A tag like the one just demonstrated can be useful in many applications where you want to output "template" text generated within the tag handler or where you want it to perform a function. For example, you could write a tag that outputs the current date and time. Beyond this, custom tags also allow you to interact with the body of a tag.

Earlier in Listing 16.1, you saw that the doStartTag method in the custom tag returns a value of SKIP_BODY and the doEndTag method returns a value of EVAL_PAGE. These values tell the JSP engine how to handle the content between the start and end of the custom tag and also whether to continue evaluating the rest of the page after the custom closing tag. When doStartTag returns SKIP_BODY, it tells the JSP engine to ignore the content between the start and end of the custom tag. If the doStartTag returns EVAL_BODY_INCLUDE, the data between the start and end tags is evaluated and the results are copied to the response.

When doEndTag returns EVAL_PAGE, it tells the JSP engine to continue evaluating the rest of the page. If doEndTag returns SKIP_PAGE, the JSP engine ignores everything else in the JSP after the closing tag and returns the response to the browser.

Because you can control whether the JSP engine includes body text between the start and end of a tag, you can create tags that include text only if certain conditions are met.

Listing 16.5 shows a custom tag that includes its content only when the time of day is between 6 a.m. and 6 p.m.

Listing 16.5 Source Code for DayTag.java
package examples.taglibs;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.util.*;

public class DayTag extends TagSupport
{
    public int doStartTag()
        throws JspException
    {
// Get the time of day.
        GregorianCalendar currTime = new GregorianCalendar();

// Get the hour of day.
        int hour = currTime.get(Calendar.HOUR_OF_DAY);

// If the time is between 6 a.m. and 6 p.m., tell the JSP engine to
// include the text between the start and end tag.
        if ((hour >= 6) && (hour <= 18))
        {
            return EVAL_BODY_INCLUDE;
        }
        else
        {
// Otherwise, ignore the body text.
            return SKIP_BODY;
        }
    }

    public int doEndTag()
    {
        return EVAL_PAGE;
    }
}

To process body content, it is necessary to make a slight change to the TLD. Instead of declaring the body content to be empty, it must now indicate that the body is meaningful. A TLD that works for this example appears in Listing 16.6

Listing 16.6 Source Code for DayTag.java
<?xml version="1.0" encoding="ISO-8859-1"?>

<taglib xmlns=http://java.sun.com/xml/ns/j2ee
      xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
      xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd
      version="2.0">
   <tlib-version>1.0</tlib-version>
   <short-name>DayTag</short-name>
   <description>
       A tag to that conditionally includes the body.
   </description>
   <tag>
     <name>DayTag</name>
     <tag-class>examples.taglibs.DayTag</tag-class>
     <body-content>jsp</body-content>
   </tag>
</taglib>

You can see that the body-content element has been changed to jsp, which causes the container to evaluate the body content as JSP source. You can instruct the container to output the body verbatim by using the value tagdependant, or cause the container to treat the body as though it had no scripting elements by declaring the body scriptless. Later in the hour, you'll see how to use tags whose bodies are scriptless.

Listing 16.7 is a sample JSP that you can use to exercise this tag.

Listing 16.7 Source Code for TestDayTag.jsp
<%@ taglib uri="/daytag" prefix="daytag" %>
<html>
<body>
<daytag:DayTag>
It's between the hours of 6 am and 6 pm!
</daytag:DayTag>
</body>
</html>
    [ Team LiB ] Previous Section Next Section