Example of tag library using TLD Generator

In this post, I show an example of a tag library where TLD Generator is used. The example library contains one tag and one function, both for generation of  a random int. NetBeans is used to create the project.

Project structure

The package myrandom contains the tag handler and the function. The TLD generator jar and the JSP API are added to the libraries of the project.

Implementation of the tag

Every time the tag is invoked a random int is generated and sent to the output of its jspContext. The tag has an attribute to indicate the bound (exclusive) of the random int.

package myrandom;

import java.io.IOException;
import java.util.Random;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import tldgen.Tag;
import tldgen.TagAttribute;

@Tag
public class RandomNumberTag extends SimpleTagSupport{
    private int bound;

    @TagAttribute(required=true, runtimeValueAllowed=true)
    public void setBound(int bound) {
        this.bound = bound;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
        Random r=new Random();
        getJspContext().getOut().print(r.nextInt(bound));
    }
    
}

The tag handler has two important annotations:

  • @Tag in the tag handler class. Because the tag doesn’t have an explicit name, the name will be the decapitalized name of the class with the suffix “Tag” removed. So in this example, the name of the tag in the descriptor will be randomNumber.
  • @TagAttribute is present in the setter of the bound attribute. The values of the annotations define that this attribute is required and that its value can be determined from an expression.

Function Implementation

The function is implemented like this:

package myrandom;

import java.util.Random;
import tldgen.Function;

public class RandomFunction {
    
    @Function
    public static int getRandomInt(){
        return new Random().nextInt();
    }
    
}

The method is annotated with @Function. The method has to be public and static for being visible from JSP pages. The name of the function is the name of the method, but other could be defined in the annotation.

Definition of the library

At this moment, the content of the project looks like this:

In order to group the tags and functions, we need to define a tag library. This is accomplished in a package-level annotation defined in the package-info.java file in myrandom (I used New Empty File wizard in NetBeans because the Class wizard doesn’t allow package-info name for a class).

And here is the content of this file:

@TagLibrary("http://random.org")
package myrandom;

import tldgen.TagLibrary;

In the above declaration, we define an URI for the library. The descriptor file will be taglib.tld by default. This may be changed in the annotation.

Compiling the project and generating the descriptor

Now, let’s compile the project in order to generate the descriptor with the Clean and Build option of the project.

To see the generated file, go to the Files window in NetBeans and navigate to the build/classes directory of the project.  The tld file will be in the subdirectory META-INF from where it’s read by the Servlet container at deployment time.

The content of the descriptor is:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib version="2.1" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tlib-version>1.0</tlib-version>
    <uri>http://random.org</uri>
    <tag>
        <name>randomNumber</name>
        <tag-class>myrandom.RandomNumberTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>bound</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.Integer</type>
        </attribute>
    </tag>
    <function>
        <name>getRandomInt</name>
        <function-class>myrandom.RandomFunction</function-class>
        <function-signature>int getRandomInt()</function-signature>
    </function>
</taglib>

Conclusion

In this example, I showed how to use TLD generator to produce automatically a Tag Library Descriptor. Most of the times, the descriptor is manually build with information already contained in the classes. With TLD generator I don’t have to write redundant information (DRY principle).

Resources

RandomNumbers NetBeans project in zip file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s