Custom Tags 2 – Skills for Simple Tag handlers


This post presents the basic skills of simple tag handling for the JSP technology. In detail, we’ll see how to:

Here’s an overview of the hierarchy. The arrows denote an IS-A relationship.

Hierarchy of Simple tag handlers

Hierarchy of Simple tag handlers

Step 1. Write a SimpleTagSupport and provide its doTag() method.

package my;
public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Hello from a simple tag handler!!");
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>simple</name>
    <tag-class>my.Simple</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple />
  </body>
</html>
Invoking a simple tag handler

Invoking a simple tag handler

Process its body

This is possible using the invoke method of JspFragment,

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().print("This is my body:<br />");
    getJspBody().invoke(null);
  }
}

as long as the tag is allowed to have a body.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>scriptless</body-content>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple>Some useful content</yo:simple>
  </body>
</html>
Accessing the body of a simple custom tag

Accessing the body of a simple custom tag

Define an attribute inside its body

Also, you may define attributes right inside the body of the tag.

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().setAttribute("friend", "Niko");
    getJspBody().invoke(null);
  }
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple>Hello ${friend}!!</yo:simple>
  </body>
</html>
Using an attribute inside the tag's body

Using an attribute inside the tag's body

Define a tag attribute

An attribute is defined as a setter method,

public class Simple extends SimpleTagSupport {
  private String friend;
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Hello " + friend + "!!");
  }
  public void setFriend(String friend) {
    this.friend = friend;
  }
}

that is also declared in the TLD.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>empty</body-content>
  <attribute>
    <name>friend</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple friend="Niko" />
  </body>
</html>
Using an attribute inside the tag's body

Using an attribute inside the tag's body

Dynamic attributes

To process such attributes you should implement the DynamicAttributes; an interface with a single method.

public class Simple extends SimpleTagSupport implements DynamicAttributes {
  private Map<String, Object> map = new HashMap<String, Object>();
  public void setDynamicAttribute(String uri, String name, Object value) {
    map.put(name, value);
  }
  public void doTag() throws IOException, JspException {
    JspWriter out = getJspContext().getOut();
    out.append("These are the dynamic attributes:");
    out.append("<ul>");
    for (Map.Entry<String, Object> element : map.entrySet()) {
      out.append("<li>");
      out.append(element.getKey() + " &rArr; " + element.getValue());
      out.append("</li>");
    }
    out.append("</ul>");
  }
}

The doTag() method simply displays the name and value of each dynamic attribute.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>empty</body-content>
  <dynamic-attributes>true</dynamic-attributes>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple friend="Niko" casoline="soft" more="lagi" />
  </body>
</html>
Displaying the dynamic attributes

Displaying the dynamic attributes

Skip the rest of the page

This is accomplished by throwing a SkipPageException at the desired place.

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Encapsulation");
    throw new SkipPageException();
  }
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    (Start of page)
    <yo:simple />
    (End of page)
  </body>
</html>
Skipping the rest of the page

Skipping the rest of the page

One Response to Custom Tags 2 – Skills for Simple Tag handlers

  1. […] Custom Tags 2 – Skills for Simple Tag handlers […]

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

%d bloggers like this: