Custom tag body

Custom tag body


Posted in : Java Posted on : February 8, 2012 at 6:58 PM Comments : [ 0 ]

In this tutorial you will learn about the custom tag's body.

Custom tag body

In this tutorial you will learn about the custom tag's body.

Contents written between the opening and closing tags of an action element is called the body content of tag which will include in the output. Body contents can be manipulated by tag handler class and it can be simply validated by the tag and included to the output. for example :

<dev:mytag>
<br>Body of custom tag</b>
</dev:mytag>

Example :

Here I am giving a simple example which will demonstrate you how to use the body of custom tag. To do so at first I have created a tag handler class named SimpleTagHandler2.java which will receives the information from the custom tag at the time of request, then created a TLD file named simpleTag2.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page named customTagBodyExample.jsp.

SimpleTagHandler2.java

package pack;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspWriter;

public class SimpleTagHandler2 extends TagSupport
{
String str;
public int doStartTag()
{
return EVAL_BODY_INCLUDE;
}
public int doEndTag()
{
return EVAL_PAGE;
}
}

simpleTag2.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>mytag2</name>
<tag-class>pack.SimpleTagHandler2</tag-class>
</tag>
</taglib>

customTagBodyExample.jsp

<%@taglib uri="/WEB-INF/simpleTag2.tld" prefix="dev" %>
<html>
<head>
<title>Custom tag Example with body</title>
<body>
<dev:mytag2>
<b>These contents are written in the body of Custom tag.</b>
</dev:mytag2>
</body>
</head>
</html>

Output :

When you will execute the customTagBodyExample.jsp page you will get the output as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics