Custom tag doStartTag()

Custom tag doStartTag()


Posted in : Java Posted on : February 8, 2012 at 7:20 PM Comments : [ 0 ]

In this tutorial you will learn about how to use the doStartTag() in tag handler class

Custom tag doStartTag()

In this tutorial you will learn about how to use the doStartTag() in tag handler class

Tag handler method doStartTag() is defined by a Tag interface that is called by the Jsp page's servlet when a start tag is evaluated. doStartTag() method is invoked when a start tag of custom tag is found. This function returns static final integer constant value that are already defined in the interface such as :

  • SKIP_BODY : It is an optional returned value but this value must be returned by doStartTag() when you want to skip the body evaluation that is it must be returned when the TagLibraryDescriptor file contains the element <body-content>empty</body-content>, the value "empty" shows that there will always be an empty action.
  • EVAL_BODY_INCLUDE : It is an optional returned value but this value must be returned by the doStartTag() when you want to evaluate the body.

Example :

Here I am giving a simple example which will demonstrate you how to use the doStartTag() tag handler method and the constant integer value. To do so at first I have created a tag handler class named DoStartTagExample.java that extends the TagSupport class ( I have used here the TagSupport class to define a tag handler class because it is easy to use, you can use the Tag interface but if you will use this interface you will be required to implement all the methods defined in it ) then I have created a TLD file named doStartTagExample.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page named doStartTagExample.jsp.

DoStartTagExample.java

package pack;

import javax.servlet.jsp.tagext.TagSupport;

public class DoStartTagExample extends TagSupport
{ 
public int doStartTag()
{
//return EVAL_BODY_INCLUDE;
return SKIP_BODY;
} 
}

doStartTagExample.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>doStart</name>
<tag-class>pack.DoStartTagExample</tag-class> 
<body-content>empty</body-content> 
</tag>
</taglib>

doStartTagExample.jsp

<%@taglib uri="/WEB-INF/doStartTagExample.tld" prefix="dev" %>
<html>
<head>
<title>doStartTag() Example</title>
</head>
<body>
Jsp page content before starting tag.
<dev:doStart>
<br><b>contents displayed after the custom tag's
start tag is encountered.</b>
</dev:doStart>
<br>Jsp page content after closing tag.
</body>
</html>

Output :

When you will execute the above jsp page you will get the output as following :

1. When you will use the constant field EVAL_BODY_INCLUDE then output will be :

2. When you will use the constant field SKIP_BODY then output will be :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics