Designing JSP Custom Tag Libraries
Pages: 1, 2
The JSP
Once your TLD and tag handlers are created, you can begin accessing the
tags in your JSP. You declare that a JSP page will use tags defined in a
tag library by including a taglib directive in the page before
any custom tag is used. The prefix attribute is a shortcut to referencing
the library throughout the page.
Sample Hello.jsp:
<%@ taglib uri="/oreillySample.tld" prefix="sample" %>
<html>
<head>
<title>Your Standard Hello World Demo</title>
</head>
<body bgcolor="#ffffff">
<hr />
<sample:hello name="Sue"/>
<hr />
</body>
</html>
The HTML source output from this JSP would look like:
<html>
<head>
<title>Your Standard Hello World Demo</title>
</head>
<body bgcolor="#ffffff">
<hr />
<table border="1">
<tr><td> Hello Sue </td></tr>
</table>
<hr />
</body>
</html>
You can see how the tag was evaluated and the contents of the tag inserted into the output stream.
If you wanted to add functionality to our tag to make it more flexible, say to evaluate a body a certain number of times and make the name attribute evaluated at runtime, you can do so fairly easily with a few changes. First you would make the following changes to the TLD file. The tag definition would look like
<tag>
<name>hello</name>
<tagclass>oreilly.examples.Hello </tagclass>
<!-- Allow for a body to be included for this tag -->
<bodycontent>JSP</bodycontent>
<info>
This is a simple hello tag.
</info>
<!-- Optional attributes -->
<!-- personalized name -->
<attribute>
<name>name</name>
<required>false</required>
<rtexpvalue>true</rtexpvalue>
</attribute>
<!-allow for the jsp coder to specify how many times to iterate -->
<attribute>
<name>iterations</name>
<required>false</required>
<rtexpvalue>false</rtexpvalue>
</attribute>
</tag>
Then you must alter the handler class by extending
TagBodySupport and implementing the body methods if you wanted
different behavior from that provided in the base class implementation. The
handler class would now look like
public class Hello extends BodyTagSupport {
private String name=null;
private int iterations=1;
/**
* Getter/Setter for the attribute name as defined in the tld file
* for this tag
*/
public void setName(String value){
name = value;
}
public String getName(){
return(name);
}
/**
* Getter/Setter for the attribute iterations as defined in the tld file
* for this tag
*/
public void setIterations(String value){
try {
iterations = Integer.parseInt(value);
} catch(NumberFormatException nfe) {
iterations = 1;
}
}
public String getIterations(){
return(Integer.toString(iterations));
}
public int doStartTag() throws JspTagException{
try {
JspWriter out = pageContext.getOut();
out.println("<table border=\"1\">");
if (name != null)
out.println("<tr><td> Hello " + name + " </td></tr>");
else
out.println("<tr><td> Hello World <td></tr>");
} catch (Exception ex) {
throw new JspTagException("All is not well in the world." + ex );
}
// Evaluate the body if there is one
return EVAL_BODY_TAG;
}
public int doEndTag()throws JspTagException {
try {
JspWriter out = pageContext.getOut();
out.println("</table>");
} catch (Exception ex){
throw new JspTagException("All is not well in the world." + ex);
}
}
public int doAfterBody() throws JspTagException {
if (iterations-- >= 1) {
BodyContent body = getBodyContent();
try {
// Make sure we put anything in the output stream in the
// body to the output stream of the JSP
JspWriter out = body.getEnclosingWriter();
out.println(body.getString());
body.clearBody(); // Clear for next evaluation
} catch(IOException ioe) {
throw new JspTagException("Error in Hello tag doAfterBody " + ioe);
}
return(EVAL_BODY_TAG);
} else {
return(SKIP_BODY);
}
}
Now let's make the simple changes in the JSP file. Our sample
Hello.jsp looks like
<%@ taglib uri="/oreillySample.tld" prefix="sample" %>
<%!
// allow a username to be passed in the request
String userName = request.getParameter("NAME");
%>
<html>
<head>
<title>Your Standard Hello World Demo</title>
</head>
<body bgcolor="#ffffff">
<hr />
<sample:hello name="<%= userName %>" iterations="2">
<tr><td><b>Have a nice day</b></td></tr>
</sample:hello>
<hr />
</body>
</html>
If our JSP page was called like hello.jsp?NAME=Sue our
output would look like
<html>
<head>
<title>Your Standard Hello World Demo</title>
</head>
<body bgcolor="#ffffff">
<hr />
<table border="1">
<tr><td> Hello Sue </td></tr>
<tr><td><b>Have a nice day</b></td></tr>
<tr><td><b>Have a nice day</b></td></tr>
</table>
<hr />
</body>
</html>
This simple example is but a small glimpse at the power of custom tag libraries. We examined how to create and use a simple tag library and then easily extend its functionality. The next article will examine some of the advanced features of tags, including defining scripting variables and cooperating tags. It will also go through working examples of each.
Sue Spielman is an associate editor for ONJava.com, covering JSP and Servlets technologies. She is also President and Senior Consulting Engineer for Switchback Software LLC.
Return to ONJava.com.
-
JSp Custom Tag Handler
2009-07-29 23:27:24 ashishscores [View]
-
I'm new to create the custom tag libraries.
2008-10-08 04:34:33 Vinayak.vb [View]
-
I'm new to create the custom tag libraries.
2008-10-08 04:31:19 Vinayak.vb [View]
-
can anyone solve my doubt
2007-11-28 09:36:45 laxmi001 [View]
-
can anyone solve my doubt
2007-11-28 09:34:44 laxmi001 [View]
-
change the tld file there is a spelling mistake in tag name
2007-09-27 04:06:09 sathya_k_83 [View]
-
error in tld
2007-09-27 03:51:54 sathya_k_83 [View]
-
How to include jsp include directive in JspWriter
2007-07-17 23:31:18 Madhukar29 [View]
-
How to include jsp include directive in JspWriter
2007-07-17 23:03:12 Madhukar29 [View]
-
Error occured while loading the class
2006-06-18 23:50:57 Greeny [View]
-
ERROR WHILE DISPLAYING JSP
2005-09-10 10:02:58 kingviju [View]
-
hello.jsp with iterations not working
2005-07-28 05:08:41 RachelMary [View]
-
Error in sample code
2005-07-15 03:31:19 Amit_J [View]
-
help needed to create a mail server
2005-03-01 19:54:35 prabhu_studs [View]
-
doEndTag
2004-07-08 12:00:38 jkronz [View]
-
Errors in the sample code
2004-06-25 00:21:09 sjsobol [View]
-
JSP Tag Libraries
2004-06-10 06:46:22 fj_rodriguez [View]
-
help trying tu run it
2003-10-29 14:56:14 anonymous2 [View]
-
Answer to: what goes where :
2003-07-02 03:41:34 anonymous2 [View]
-
what goes where?
2003-03-02 13:03:52 ahuimanu [View]
-
corrected files (including web.xml)
2003-02-14 09:59:11 anonymous2 [View]
-
Wow Wouldn't it be nice if we provided a working example
2003-02-04 09:19:20 anonymous2 [View]
-
Missing a return value for doEndTag()
2002-01-20 05:11:34 taranjeet [View]
-
Code for doEndTag() is missing a return value
2001-09-19 09:53:08 violinner [View]
-
Error while compiling the jsp file
2001-08-08 21:55:58 babloosony [View]
-
Typo in TLD file
2001-08-08 05:24:16 lpleva [View]
-
repost of typo correction
2001-05-30 15:36:16 mdwchang [View]
-
Typo in DoStartTag?
2001-05-30 15:34:14 mdwchang [View]
-
Error when using Custom Tags
2001-05-30 07:42:27 msprakasam [View]
-
Error when using Custom Tags
2001-05-30 07:41:57 msprakasam [View]
-
Parse Error in the tag library descriptor
2001-05-29 03:39:08 pguillebaud [View]
-
Missing return statement in doEndTag example
2001-04-25 13:33:53 webarch [View]
-
Missing return statement in doEndTag example
2001-04-25 13:33:53 webarch [View]