[ Team LiB ] Previous Section Next Section

Accessing Tag Attributes

Just like regular tags, custom tags can have attributes. You need to provide get and set methods for each attribute. Although you enclose each attribute in quotes, you can have numeric attributes in your custom tag. The JSP engine performs the conversion automatically. Listing 16.9 shows a custom tag to display the first and last name of a person. The name displayed can be changed by attributes in the <name> tag.

Listing 16.9 Source Code for NameTag.java
package examples.taglibs;

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

public class NameTag extends TagSupport
{
    protected String firstName = "First";
    protected String lastName = "Last";

    public int doStartTag()
        throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            out.println(firstName + " " + lastName);
        }
        catch (IOException ioExc)
        {
            throw new JspException(ioExc.toString());
        }

        return SKIP_BODY;
    }

    public int doEndTag()
    {
        return EVAL_PAGE;
    }

// Get/set methods, just like in a bean

    public String getFirstName() { return firstName; }
    public void setFirstName(String aName) { firstName = aName; }

    public String getLastName() { return lastName; }
    public void setLastName(String aName) { lastName = aName; }

}

Now, just putting the attributes in the tag is not enough. You must also configure the attributes in the TLD file. An <attribute> tag is used to define each attribute. The only required element within the <attribute> tag is a <name> tag that defines the name of the attribute. Several optional elements include the following: a <required> tag indicating whether the attribute is required; a <type> tag that defines the runtime type of the attribute; a <description> tag that is used to describe the attribute; and a tag called <rtexprvalue>. You might have noticed other JSP examples where a JSP expression (the <%= tag) was used to specify the value of a tag attribute. Evaluating custom tags where the attribute value can be generated at runtime is a difficult task for the JSP engine. Rather than allow all attribute expressions to be computed at runtime, the JSP engine wants you to explicitly mark the attributes whose values can be generated at runtime. Set the value of <rtexprvalue> to yes or true if you need the attribute to be evaluated at runtime. The value is false by default.

For the <required> tag, you can use values of true or false. The <required> tag is false by default, meaning that if you don't explicitly say otherwise, an attribute is optional.

A little later in this hour, we'll cover another subelement, the <fragment> tag. This element is used with newer constructs of custom tags.

Listing 16.10 shows the TLD file for the NameTag class.

Listing 16.10 Source Code for name.tld
<?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>name</short-name>
   <description>
       An example of a tag that uses attributes.
   </description>
   <tag>
     <name>name</name>
     <tag-class>examples.taglibs.NameTag</tag-class>
        <attribute>
            <name>firstName</name>
            <required>no</required>
        </attribute>
        <attribute>
            <name>lastName</name>
            <required>no</required>
        </attribute>
     <body-content>empty</body-content>
   </tag>
</taglib>

The name.tld file is similar to the other TLD files you have seen, except that this one defines attributes for its tag.

    [ Team LiB ] Previous Section Next Section