custom tags in jsp pages

25
Custom Tags in JSP Pages Mohan Bang

Upload: mohan-bang

Post on 25-May-2015

1.104 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Custom tags in jsp pages

Custom Tags in JSP Pages

Mohan Bang

Page 2: Custom tags in jsp pages

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.

Page 3: Custom tags in jsp pages

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.

Page 4: Custom tags in jsp pages

Usage

Page 5: Custom tags in jsp pages

• 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.

Page 6: Custom tags in jsp pages

Types of Tags

Page 7: Custom tags in jsp pages

Simple Tags

<tt:simple />

Tags With Attributes

<logic:present parameter="Clear">

Tags with Bodies

<first:tolowercase>

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

Page 8: Custom tags in jsp pages

To Defining a Tags

Page 9: Custom tags in jsp pages

• 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)

Page 10: Custom tags in jsp pages

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

Page 11: Custom tags in jsp pages

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

Page 12: Custom tags in jsp pages

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

Page 13: Custom tags in jsp pages

Create the Tag Handler Class

Page 14: Custom tags in jsp pages

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; } }

Page 15: Custom tags in jsp pages

• 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.

Page 16: Custom tags in jsp pages

Create a Tag Library Descriptor

Page 17: Custom tags in jsp pages

<?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>

Page 18: Custom tags in jsp pages

Define a reference of tag library in web.xml file

Page 19: Custom tags in jsp pages

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

Page 20: Custom tags in jsp pages

Using The Tag

Page 21: Custom tags in jsp pages

<%@ taglib

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

prefix="first"

%>

<HTML>

<BODY >

<first:hello/>

</BODY>

</HTML>

Page 22: Custom tags in jsp pages

Links

• http://displaytag.sourceforge.net/

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

Page 23: Custom tags in jsp pages

• ATag t = new ATag();

• t.setPageContext(...);

• t.setParent(...);

• t.setAttribute1(value1);

• t.setAttribute2(value2);

• t.doStartTag();

• t.doEndTag();

• t.release();

Page 24: Custom tags in jsp pages

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();

Page 25: Custom tags in jsp pages

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