custom tags in jsp pages

Post on 25-May-2015

1.104 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Custom Tags in JSP Pages

Mohan Bang

JSP custom tags are merely Java classes that implement special interfaces. Once they are developed and deployed, their actions can be called from your jsp’s using XML syntax.

They have a start tag and an end tag.

They may or may not have a body.

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed.

Usage

• Operations on implicit objects• Processing forms• Accessing databases • Accessing enterprise services such as e- mail and directories and performing flow control. • To reduce or eliminate scriptlets in your JSP applications.

Types of Tags

Simple Tags

<tt:simple />

Tags With Attributes

<logic:present parameter="Clear">

Tags with Bodies

<first:tolowercase>

Welcome to JSP Custom Tags Programming. </first:tolowercase>

To Defining a Tags

• Develop a tag handler and helper classes for the Tag• Declare the tag in a tag library descriptor• Define a reference to the tag library descriptor in the web-application descriptor (web.xml)

Tag Handlers

A tag handler is an object invoked by a Webcontainer to evaluate a custom tag during theexecution of the JSP page that references the tag.

javax.servlet.jsp.tagext.Tagjavax.servlet.jsp.tagext.BodyTag

javax.servlet.jsp.tagext.TagSupportjavax.servlet.jsp.tagext.BodyTagSupport

public interface Tag {

public final static int SKIP_BODY = 0;

public final static int EVAL_BODY_INCLUDE = 1;

public final static int SKIP_PAGE = 5;

public final static int EVAL_PAGE = 6;

void setPageContext(PageContext pageContext);

void setParent(Tag parent);

Tag getParent();

int doStartTag() throws JspException;

int doEndTag() throws JspException;

void release();

}

Tag Interface

BodyTag Interface

• Void doInitBody()           Prepare for evaluation of the body.

• Void setBodyContent(BodyContent b)           Set the bodyContent property.

• doAfterBody() From Iteration Interface

• All the methods from the Tag Interface

Create the Tag Handler Class

import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;

public class HelloTag extends TagSupport { public int doStartTag() throws JspException { try { pageContext.getOut().print("This is my first tag!"); } catch (IOException ioe) { throw new JspException("Error:

IOException while writing to client" + ioe.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException { return SKIP_PAGE; } }

• A tag handler has access to an API that allows it to communicate with the JSP page. The entry point to the API is the page context object (javax.servlet.jsp.PageContext), through which a tag handler can retrieve all the other implicit objects (request, session, and application) accessible from a JSP page.

• Implicit objects can have named attributes associated with them. Such attributes are accessed using [set|get]Attribute methods.

• If the tag is nested, a tag handler also has access to the handler (called the parent) associated with the enclosing tag.

Create a Tag Library Descriptor

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.// DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/ web-jsptaglibrary_1_1.dtd">

<taglib> <tlibversion>1.0</tlibversion>

<jspversion>1.1</jspversion> <shortname>first</shortname>

<uri></uri> <info>A simple tab library for the examples</info> <tag> <name>hello</name> <tagclass>tags.HelloTag</tagclass> <bodycontent>empty</bodycontent> <info>Say Hi</info> </tag> </taglib>

Define a reference of tag library in web.xml file

<taglib> <taglib-uri> mytags </taglib-uri> <taglib-location> /WEB-INF/jsp/ mytaglib.tld </taglib-location> </taglib>

Using The Tag

<%@ taglib

uri="/WEB-INF/jsp/mytaglib.tld"

prefix="first"

%>

<HTML>

<BODY >

<first:hello/>

</BODY>

</HTML>

Links

• http://displaytag.sourceforge.net/

• http://www.jspin.com/home/tags/

• ATag t = new ATag();

• t.setPageContext(...);

• t.setParent(...);

• t.setAttribute1(value1);

• t.setAttribute2(value2);

• t.doStartTag();

• t.doEndTag();

• t.release();

t.doStartTag(); out = pageContext.pushBody(); t.setBodyContent(out);// perform any initialization needed after body content is set t.doInitBody(); t.doAfterBody(); // while doAfterBody returns EVAL_BODY_BUFFERED we // iterate body evaluation ... t.doAfterBody(); t.doEndTag(); t.pageContext.popBody(); t.release();

For each tag attribute, you must specify whether the attribute is required, whether the value can be determined by an expression, and, optionally, the type of the attribute in an attribute element. For static values the type is always java.lang.String. If the rtexprvalue element is true or yes, then the type element defines the return type expected from any expression specified as the value of the attribute.

<attribute>

<name>attr1</name>

<required>true|false|yes|no</required>

<rtexprvalue>true|false|yes|no</rtexprvalue>

<type>fully_qualified_type</type>

</attribute>

attribute Element

top related