JSP : Creating Custom Tag

21
CUSTOM TAG LIBRARY Prof. AshishSingh Bhatia 1 Prof. AshishSingh Bhatia

description

Learn how to create the custom tag in JSP using SimpleTagSupport. Two approaches Java Based Custom Tag or JSP Based Custom Tag.

Transcript of JSP : Creating Custom Tag

Page 1: JSP : Creating Custom Tag

CUSTOM TAG LIBRARY

Prof. AshishSingh Bhatia

1Prof. AshishSingh Bhatia

Page 2: JSP : Creating Custom Tag

Two Approach

Java Based Custom Tag

No Version Restriction

Uses Java file for Tag Handling

Preferred when lot of java code is required for getting output

JSP Based Custom Tag

Only from JSP 2.0

Uses JSP file for Tag Handling

Preferred when lot of html code is required for getting output.

Prof. AshishSingh Bhatia 2

Page 3: JSP : Creating Custom Tag

Tag Library Components

Tag handler class that defines the tag behavior.

The TLD file that maps the XML element names to the tag implementation.

The JSP file that uses the tag library.

3Prof. AshishSingh Bhatia

Page 4: JSP : Creating Custom Tag

The Tag Handler Class

Class that tells what to do when system see the tag.

Class must implement SimpleTag interface.

In practice, extends SimpleTagSupport which implements SimpleTag.

javax.servlet.jsp.tagext package.

Every Tag Handler class must have 0 argument constructor.

doTag() is the main method for tag handling.

We need JspWriter [ getJspContext().getOut() ]

New instance is created for every tag occurrence on the page.

Prof. AshishSingh Bhatia 4

Page 5: JSP : Creating Custom Tag

Directory Structure

hellowordtag

index.jsp

WEB-INF

tlds tag.tld

classes mytag HelloWorldTag.java

Prof. AshishSingh Bhatia 5

Page 6: JSP : Creating Custom Tag

Tag Handler Class

package mytag;

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.*;

public class HelloWorldTag extends SimpleTagSupport {

public void doTag() throws JspException, IOException {

JspWriter out = getJspContext().getOut();

out.print("<b>Hello World</b>");

}

}

Prof. AshishSingh Bhatia 6

Page 7: JSP : Creating Custom Tag

TLD : Tag Library Descriptor

<?xml version="1.0" ?>

<taglib version="2.0">

<tlib-version>1.0</tlib-version>

<short-name>mytag</short-name>

<tag>

<description>HelloWorld Tag</description>

<name>helloworld</name>

<tag-class>mytag.HelloWorldTag</tag-class>

<body-content>empty</body-content>

</tag>

</taglib>

Prof. AshishSingh Bhatia 7

Optional

Required Element

empty, scriptless, tagdependent, JSP

Page 8: JSP : Creating Custom Tag

JSP File

<html>

<head>

<title>Tag Example</title>

</head>

<body>

<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>

<ashish:helloworld/>

</body>

</html>

Prof. AshishSingh Bhatia 8

Page 9: JSP : Creating Custom Tag

Assigning Attributes to Tags

For every attribute we need a set method in tag handler class

<prefix:tag attribute1=“value1” attribute2=“value2” … />

Prof. AshishSingh Bhatia 9

setAttribute1(String value) setAttribute2(String value)

Page 10: JSP : Creating Custom Tag

TLD File

<attribute>

<description>…. </description>

<name> X </name>

<required> true/false </required>

</attribute>

Prof. AshishSingh Bhatia 10

X = Must match with the variable in class

Page 11: JSP : Creating Custom Tag

Example : Custom tag to reverse a String

public class StringReverseTag extends SimpleTagSupport {

private String data;

public void setData(String data) {

this.data=data;

}

public void doTag() throws JspException, IOException {

JspWriter out = getJspContext().getOut();

StringBuffer sb = new StringBuffer(data);

sb.reverse();

out.print(sb);

}

}

Prof. AshishSingh Bhatia 11

Page 12: JSP : Creating Custom Tag

TLD File

<tag>

<description>StringReverse Tag</description>

<name>string</name>

<tag-class>mytag.StringReverseTag</tag-class>

<body-content>empty</body-content>

<attribute>

<name>data</name>

<required>true</required>

</attribute>

</tag>

Prof. AshishSingh Bhatia 12

Page 13: JSP : Creating Custom Tag

JSP FILE

<html>

<head>

<title>Tag Example</title>

</head>

<body>

<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>

<ashish:string data="EARTH"/>

</body>

</html>

Prof. AshishSingh Bhatia 13

Page 14: JSP : Creating Custom Tag

Including Tag body in Tag Output

getJspBody().invoke(null)

null means the resulting output of that JSP content is passed verbatim to the client.

doTag() has no way to access the tag body output.

Example In JSP File : <ashish:tag> This is the test </ashish:tag>

In Java File

out.print(“<b>Hello World</b> <br/>”);

getJSPBody().invoke(null);

out.print(“<b>This is my tag</b>”);

In Tag File : <body-content>scriptless</body-content>

Prof. AshishSingh Bhatia 14

Page 15: JSP : Creating Custom Tag

Using Tag Files

Java Based Custom Tag What we have seen is Java Based Custom Tag

Tag handler class is Java File

JSP Based Custom Tag [ Tag Files ] Tag Handler class is JSP file

When to use which ?

Simple Rule : Use Java Based Custom Tag when lot of java code is involved. IF more is of formatting use JSP Based Custom Tag.

Remember Tag files run only on JSP 2.0.

Java Base Tag have no such restriction.

Prof. AshishSingh Bhatia 15

Page 16: JSP : Creating Custom Tag

JSP Based Custom Tag

Create a JSP Base tag file

Create a JSP page that uses the tag file

Prof. AshishSingh Bhatia 16

Page 17: JSP : Creating Custom Tag

Structure

tagdemo

index.jsp

WEB-INF tags helloworld.tag

Prof. AshishSingh Bhatia 17

Page 18: JSP : Creating Custom Tag

helloworld.tag and index.jsp

Tag File [ helloworld.tag ]

<b>Hello World</b>

JSP File

<html>

<head><title>Tag Example</title></head>

<body>

<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>

<ashish:helloworld/>

</body>

</html>

Prof. AshishSingh Bhatia 18

Page 19: JSP : Creating Custom Tag

String Reverse [ Using Attribute and Tag File ]

Tag File [ reverse.tag ]

<%@ attribute name="data" required="true" %>

<%

StringBuffer sb = new StringBuffer(data);

sb.reverse();

%>

<%= sb %>

JSP File

<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>

<ashish:reverse data="EARTH"/>

Prof. AshishSingh Bhatia 19

Page 20: JSP : Creating Custom Tag

Using Body part of the tag using Tag File

Use <jsp:Body/> to get the out put of body

Example In JSP File : <ashish:test> This is the test </ashish:test>

In Tag File [ test.tag ] :

<b>Hello World</b> <br/>

<jsp:doBody/>

<b>This is my tag</b>

Prof. AshishSingh Bhatia 20

Page 21: JSP : Creating Custom Tag

END OF SESSION

21Prof. AshishSingh Bhatia