<taglib:tutorial lesson="12">

In this lesson, we will take a closer look at the Tag Library descriptor changes between JSP 1.1 and JSP 1.2.

1 Lesson 12, Changes to the TLD

    In this lesson, we will take a closer look at the interesting parts of the Tag Library descriptor changes between JSP 1.1 and JSP 1.2. We will not write any code in this lesson, but instead use parts of the Tag descriptor from lesson 10, lesson 11 and lesson 12 as an example. You should therefore have finished lesson 10 before continuing on this one.

2 The new variable tag

    Back in JSP 1.1, one would have to describe any variables that were to be added through a VariableInfo class held by a TEI implemented for this particular Tag (this is covered in greater detail in lesson 5). This unfortunate implementation style led to that a lot of new deveopers gave up on writing custom Tags as they found it too troublesome. Therefore, the expert group of JSP 1.2 added the ability to define simple VariableInfo implementations directly in the TLD, which can look like in listing 1 below:.


    ...
    <taglib>
    ...
    <tag>
    ...
    <variable>
    <name-from-attribute>id</name-from-attribute>
    <variable-class>com.acme.bean.Contact</variable-class>
    <declare>true</declare>
    <scope>NESTED</scope>
    </variable>
    ...
    </tag>
    ...
    </taglib>
    Listing 1: Example of Tag description with variable.

    Take a good look at the <variable> part above, and then compare it to the VariableInfo class below:


    Figure 1: The VariableInfo class

    The observant reader will notice that the <variable> tag added in JSP 1.2 holds the data of the old VariableInfo class that was added in JSP 1.1 and some new items.

    Let's have a look at the tags involved:

    1. The <name-from-attribute> tag (new in JSP 1.2) tells the Container that the actual name of the variable that will be added will be specified by one of the Tags attributes at translation time, in this case "id". If the name is known beforehand, one could use the <name-given> tag to specify the constant name instead.

    2. <variable-class> (compare with VariableInfo.getClassName()) is used to specify what kind of variable that will be added. In the example above it's a Contact bean. The default value is java.lang.String.

    3. <declare> (compare with VariableInfo.getDeclare()) specified if the variable is to be defined or not. The default is "true" (we specify it just to show it off). Valid values are true, false, yes and no.

    4. <scope> (compare with VariableInfo.getScope()) specifies the scope of the variable defined, the default is "NESTED" (we specify it just to show it off). Valid values are AT_BEGIN, AT_END and NESTED. These values are discussed in lesson 5 (see the description of the VariableInfo class).

    This might look like a minor addition, but it has a major impact on development time.

    Refer to lesson 17 to get an example usage of the new variable element.

3 The modified attribute tag

    The attriute tag has been modified in JSP 1.2 so that the Tag developer can now tell the container what type to expect for a certain attribute, thereby sparing the Container from guessing. This might look as in the example below:


    ...
    <taglib>
    ...
    <tag>
    ...
    <attribute>
    <name>counter</name>
    <type>int<type>
    <required>true</required>
    </attribute>
    ...
    </tag>
    ...
    </taglib>
    Listing 2: Example of Tag description with attribute.

4 The name changes

    The JSP 1.2 Tag library descriptor has changed the name of most old tags to better comply with the other J2EE descriptor files. Most of these changes boils down to separating words with a dash. For instance, the tag named "tagclass" in JSP 1.1 is now known as "tag-class".

5 The new validator tag

    Back in JSP 1.1, Tag developers could put translation-time validation into the isValid() method of a TEI class (see lesson 5), and that method could use the information held by the TagData given to it to validate the tag usage.

    The JSP 1.2 Tag library descriptor now allows for a TagLibraryValidator class to be specified for a Tag library, so that this validator is invoked as soon as a page is compiled that utilizes the Tag Library that the validator is part of. The TagLibraryValidator is described within a <validator> tag, as exemplified in listing 3 below:


    ...
    <taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>mt2</short-name>
    <uri>http://www.orionserver.com/tutorials/mytags2</uri>
    <description>My Second Tag library</description>
    <validator>
    <validator-class>com.acme.tag.validator.SampleValidator</validator-class>
    <init-param>
    <param-name>enabled</param-name>
    <param-value>false</param-value>
    <description>turning the validator on or off</description>
    </init-param>
    <description>my sample validator</description>
    </validator>
    ...
    </taglib>

    Listing 3: Example of Tag description with validator.

    When a JSP page that utilizes a Tag Library containing a TagLibraryValidator is compiled, the Container will execute the validate() method of the specified validator. As this is covered in lesson 14, we wont go into any details here.

    The <validator-class> tag contains the fully qualified name of the TagLibraryValidator implementation.

    The <init-param> tag can be used to specify any number of parameters that should be passed to the validator during initialization.

    The <description> tag is used to enter a optional description of the TagLibraryValidator class.

6 The new listener tag

    The JSP 1.2 Tag library descriptor can hold an event listener that will be instantiated and registered automatically. Web application listener beans was introduced in Servlet 2.2 (at that time one could include HttpSessionBindingListener implementations in Web-modules) and was further extended in Servlet 2.3 with the additions of ServletContextAttributeListener, ServletContextListener, HttpSessionActivationListener and HttpSessionAttributeListener. The event listener implementation is described within the <listener> tag as exemplified below:


    ...
    <taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>mt2</short-name>
    <uri>http://www.orionserver.com/tutorials/mytags2</uri>
    <description>My Second Tag library</description>
    <listener>
    <listener-class>com.acme.tag.listener.SimpleContextListener</listener-class>
    </listener>
    ...
    </taglib>
    Listing 4: Example of Tag description with listener.

    Listeners will be covered in lesson 13, "Using Listeners", which happends to be the next lesson.

Copyright © 2005 IronFlare AB