Loading...

J2EE Custom Simple Tags Part 1

Software
May 9, 2007
#java
Avatar
Author
Matt Kruskamp

Note: This article assumes you have done some Java web development in the past, and you are capable of compiling and executing web applications with J2EE.

Overview

J2EE has done some amazing things in progressing the creation of custom tags. Now it is fairly easy to integrate any custom HTML tag within your application further separating the view from designers. This article will show you how to implement a custom HTML tag that calculates the square of a number by simply placing <math:square num=“12” /> into your web application. Hopefully, you can feel the power with this simple feature.

Creating the class

Every custom tag extends SimpleTagSupport. This is a class implemented by the J2EE spec which allows a class to output information to a jsp. So, lets get right to coding. Create a class in your web application that extends javax.servlet.jsp.tagext.SimpleTagSupport. If you are compiling by hand, the class file must go in the package structure under /WEB-INF/classes/ i.e. /WEB-INF/classes/com/cyberkruz/test/NewMath.class

package com.cyberkruz.test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class NewMath extends SimpleTagSupport {
    private Integer num;
    /**
    * Method overridden which is called
    * by the jsp.
    */
    public void doTag() throws JspException, IOException {
        // Gets the jsp context and prints the
        // number squared.
        this.getJspContext().getOut().print(this.num * this.num);
    }
    /**
    * Sets a number that our custom
    * tag squares.
    * @param num The number to which
    * we want to square.
    */
    public void setNum(Integer num) {
        this.num = num;
    }
}

Every attribute in your custom tag must have a Java Bean mutator property to go with it. This is handled with reflection to send the parameter to your class.

Configuring the application to use the class

Now, all we have to do is let our application know that the new tag is there. To do so, we can set up a TLD file which looks much like the document descriptor for configuring servlets. So lets do it! Create a new file called MyTags.tld and place it in the /WEB-INF directory. Then type the following:

<?xml version=“1.0” encoding=“iso-8859-1” ?>
<taglib xmlns=“http://java.sun.com/xml/ns/j2ee”
        xmlns:xsi=“http://www.w3c.org/2001/XMLSchema-instance”
        xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd”
        version=“2.0”>
  <tlib-version>1.2</tlib-version>
  <uri>myTags</uri>
  <tag>
    <description>Custom tags</description>
    <name>Square</name>
    <tag-class>com.cyberkruz.test.NewMath</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
      <name>num</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

This file configures our web application to use our new custom tag. Multiple tags can be placed and mapped with this single file. The taglib directive just specifies the schemas for this particular file and version. The uri specifies what location these tags are being mapped to. Under the tag element, the description and name should be pretty straight forward. The tag-class element points this tag to the class that we wrote previously. the body-content is stating that we don’t want inline scripting for this element. Now the attribute tag is fairly interesting. While optional, it maps the num variable to our setter in the class we specified. Then, it says we must have it there (required element) and it can be specified at run time (rtexprvalue).

Now all we have to do is use it!

Place our new tag in a jsp

Allowing our jsp to use the custom tag is the easiest part. It only requires a single declaration.

<%@ taglib prefix=“math” uri=“myTags” %>

The taglib keyword tells the jsp what we are trying to do. The prefix is what we want to put before the tags when we call them, and the uri points to the uri specified in the tld file. This is going to load every tag that is configured in that tld file that we made previously.

Now, let’s put it in a jsp. Create a jsp file called SimpleTest.jsp and place it in your web application. Place the following in your jsp:

<%@page language=“java”
contentType=“text/html; charset=ISO-8859-1”
pageEncoding=“ISO-8859-1”%>
<%@ taglib=“” prefix=“math” uri=“myTags” %>
<html>
  <head>
    <title>SimpleTest</title>
    <meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>
    </head>
  <body>
    10 squared is: <math:Square num=“10” />.
  </body>
</html>

Run it on your web server and… YAY! This is a base introduction to what you can do with custom simple tags. More information about how to use these is below. Enjoy!

More information

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags7.html

4 min read
Share this post:

Comments

comments powered by Disqus

Related Articles

All articles
Software Feb 16, 2007

Differences between Java 5 and 6

Abstract This article contains information about the differences between the Java 5 and Java 6 frameworks. More specifically, it highlights the new features added with the latest release of Java 6.

The pixel mine

I'm currently the Co-Founder / CTO with an amazing team at Rentler. We build property management software for independant landlords and their renters using a plethora of technologies.

View work
Illustration Illustration
Top