Custom tag with attributes

Custom tag with attributes


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

In this tutorial you will learn about how to use attributes with custom tags.

Custom tag with attributes

In this tutorial you will learn about how to use attributes with custom tags.

Custom tag attributes are used to provide the requested information to the tag handler class. A custom tag can have either no attributes or many attributes. These attributes are used after the tag name in the start tag and they contains a name and its respective value, these tag attributes are customizing the custom tag behavior as the parameters customized the behavior of a method, more than one attributes can be separated by giving a white space. Tags attributes are specified in a TLD (.tld) file as for example :

<tag>
<name>tagName</name>
<tag-class>package.tagHandlerClassName</tag-class>
<attribute>
<name>attrName</name>
</attribute>
</tag>

Example :

Here I am giving a simple example which will demonstrate you how to use the attribute with custom tag. To do so at first I have created a tag handler class named SimpleTagHandler1.java which will receives the information from the custom tag at the time of request, then created a TLD file named simpleTag1.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page at which used the <%@taglib> directive with attribute prefix 'attr' to create a custom tag and the uri="/WEB-INF/simpleTag1.tld" to give the custom tag's name and in the body of the jsp page created a custom tag using the prefix 'attr' and the name 'mytag1' with the attribute named str which value is "This is an attribute value", this value will be passed to the tag handler class and in tag handler class will first set this value using the setter method setStr() and return as a response using getter method getStr().

SimpleTagHandler1.java

package pack;

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

public class SimpleTagHandler1 extends TagSupport
{
String str;
public int doStartTag()
{
JspWriter jw = pageContext.getOut();
try
{
if(str!=null)
{
jw.println("Body of doStartTag()<br>");
jw.println(str);
} 
}
catch(Exception e)
{
System.out.println(e);
}
return EVAL_BODY_INCLUDE;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
} 
}

simpleTag1.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<tag>
<name>mytag1</name>
<tag-class>pack.SimpleTagHandler1</tag-class>
<attribute>
<name>str</name>
</attribute> 
</tag>
</taglib>

customTagAttributeExample.jsp

<%@taglib uri="/WEB-INF/simpleTag1.tld" prefix="attr" %>
<html>
<head>
<title>Custom tag Example with attribute</title>
<body>
<attr:mytag1 str="This is an attribute value"/>
</body>
</head>
</html>

Output :

When you will execute the jsp page customTagAttributeExample.jsp 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