Creating custom JSP tags

Posted on 04-01-2013 12:46 by graham
This tutorial describes how to create your own custom JSP tags in Java applications. As an example, we will create a tag that displays a list of action messages.

Prerequisites


This tutorial assumes you have a basic Web application created.

Creating custom tag implementation


We create a custom tag implementation by implementing the Tag interface.
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class ActionErrors implements Tag, Serializable
{
private static final long serialVersionUID = 7981162057964780076L;
private Tag parent;
private PageContext pageContext;
private List<String> errors;

@Override
public int doEndTag() throws JspException
{
return EVAL_PAGE;
}

@Override
public int doStartTag() throws JspException
{
try
{
if (this.errors != null && !this.errors.isEmpty())
{
JspWriter page = pageContext.getOut();
page.write("><ul>");

for (String error : this.errors)
{
page.write("<li>" + error + "</li>");
}

page.write("</ul></div>");
}
}
catch (IOException e)
{
throw new JspTagException("An IOException occurred.");
}
return SKIP_BODY;
}

@Override
public Tag getParent()
{
return parent;
}

@Override
public void release()
{
this.pageContext = null;
this.parent = null;
this.errors = null;
}

@Override
public void setPageContext(PageContext pc)
{
this.pageContext = pc;
}

@Override
public void setParent(Tag parent)
{
this.parent = parent;
}

public void setErrors(List<String> errors)
{
this.errors = errors;
}

public List<String> getErrors()
{
return errors;
}
}


Creating tag definitions in the TLD file


In your project directory structure, create folder tld under /src/main/webapp/WEB-INF. In this directory, create a file with the name that best describes a group of tags you want to create. We will call this file my-tags.tld. The contents of the file looks as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>My Tags</shortname>
<uri>http://www.wordgraphs.com</uri>
<info>Action errors</info>

<tag>
<name>errors</name>
<tagclass>com.mycompany.ActionErrors</tagclass>
<bodycontent>empty</bodycontent>
<info>Action Errors</info>

<attribute>
<name>errors</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

NOTE: In earlier versions of the Spring Framework it was necessary to put a reference to the TLD file in your web.xml file. With Spring 3.0, this is no longer necessary.

Using your custom tag


If you want to use your custom tag, you need to tell JSP where to look for its definition. You do this by adding the following at the beginning of your JSP file:
<%@ taglib uri="/WEB-INF/tld/my-tags.tld" prefix="mytags" %>

Now, you can use the tag the following way:
<mytags:errors errors="${errorMessages}" />

That's it!
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems