JSP Tag Files: Implementation, Directives, and Examples, Slides of Java Programming

An overview of jsp tag files, their implementation using simple tag handlers, and the use of directives such as taglib, include, tag, attribute, and variable. It also includes examples of simple and complex tag files, and the evaluation of fragments passed to tag files.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Repeat Tag Implemented as a
Simple JSP 2.0 Tag Extension
<%@ taglib prefix="my"
uri="/mytags" %>
<my:repeat num="3">
tag body
</my:repeat>
void doTag() {
for( int i = 0; i < n um; i++ ) {
getJspBody().invoke( null );
}
}
SimpleTag
SimpleTagSupport
setNum()
doTag()
RepeatHandler
Usage Implementation
ThisslideshowsrepeattagimplementationusingSimpleTaghandlerofJSP2.0.Herethe
RepeatHandlersimplyextendsSimpleTagSupportclass,whichitselfisanimplementationofSimpleTag
interface.
Nowusingsimpletaghandler,allyouhavetoimplementisdoTag()methodinwhichyouJspFragment
objectbycallinggetJspBody()methodandtheninvoke(null)methodinordertospitoutthe
JspFragmenttotheoutputwriter.

docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download JSP Tag Files: Implementation, Directives, and Examples and more Slides Java Programming in PDF only on Docsity!

Repeat Tag Implemented as a

Simple JSP 2.0 Tag Extension

<%@ taglib prefix="my" uri="/mytags" %> <my:repeat num="3"> tag body </my:repeat>

void doTag() { for( int i = 0; i < num; i++ ) { getJspBody().invoke( null ); } }

SimpleTag

SimpleTagSupport

setNum() doTag()

RepeatHandler

Usage

Implementation

This slide shows repeat tag implementation using Simple Tag handler of JSP 2.0. Here the RepeatHandler simply extends SimpleTagSupport class , which itself is an implementation of SimpleTag interface.

Now using simple tag handler, all you have to implement is doTag() method in which you JspFragment object by calling getJspBody() method and then invoke(null) method in order to spit out the JspFragment to the output writer.

Tag Files

Tag files is another significant improvement of JSP 2.0.

Tag Files

Simple yet flexible packaging Just drop a .tag file in /WEB-INF/tags/ Implicit tag library automatically generated Or, write a .tld for added flexibility ` Or, package in a JAR with a .tld

Another important feature of tag file is that it enables simpler deployment than class Java tag handler model. That is, in order deploy tag files, all you have to do is to drop tag files in /WEB‐INF/tags directory. The container is then deploy the tag files by creating implicit tag library. If more flexible deployment is desired, then you can still create TLD file.

Declaring a tag library: tagdir attribute in

taglib directive

Identifies the location of the tag files Value of it must start with /WEB-INF/tags/ Syntax <%@ taglib prefix="tt" tagdir=/WEB-INF/tags/dir %>

Tag file concept is introduced in JSP 2.0. Tag file is tag handler implementation in JSP syntax as opposed to using Java programming language.

The value of tagdir attribute starts with /WEB‐INF/tags as shown in the slide above.

“attribute” Directive attributes

description name required rtexprvalue type fragment (default is false) if true, container fixes the rtexprvalue attribute at true the type attribute at javax.servlet.jsp.tagext.JspFragment Otherwise, it is a normal attribute to be evaluated by the container prior to being passed to the tag handler

Example 1: Simple Attribute (panel.tag) <%@ attribute name="color" %> <%@ attribute name="bgcolor" %> <%@ attribute name="title" %>

${title}

The Duke's Bookstore shipDate tag, defined in shipdate.tag, is a custom tag with a simple attribute. The tag generates the date of a book order according to the type of shipping requested. The tag determines the number of days until shipment from the shipping attribute passed to it by the page bookreceipt.jsp. From the days, the tag computes the ship date. It then formats the ship date.

panel.jsp

panel.jsp

First panel.
Second panel.
Second panel.
Second panel.
Second panel.
Third panel.
A panel in a panel. Third panel.

“variable” Directive

` Declares EL variables

EL variables EL variables emulate OUT (from tag file to calling page) type while Tag attributes emulate IN (from calling page to tag file) type not initialized by the calling page set by the tag file

<%@ variable name-given="price" %> <%@ attribute name="normalPrice" fragment="true" %>

“variable” Directive attributes

description name-given | name-from-attribute Defines an EL variable to be used in the page invoking this tag Name-given : the value is the name of the variable **name-from-attribute:** the value is the name of an attribute whose (translation-time) value at of the start of the tag invocation will give the name of the variable alias variable-class declare ` scope

“variable” directory can have several attributes of its own.

name‐given: name‐from‐attribute ‐ Defines an EL variable to be used in the page invoking this tag. Either name‐given or name‐from‐attribute must be specified. If name‐given is specified, the value is the name of the variable. If name‐from‐attribute is specified, the value is the name of an attribute whose (translation‐time) value at of the start of the tag invocation will give the name of the variable.

Evaluating Fragments passed to Tag

Files

Result of evaluation is sent to the response or is stored in an EL variable for later manipulatipn var : type String **varReader** : type java.io.Reader scope attribute (optional) indicates the scope of resulting variable page (default) request session application

The result of evaluating either type of fragment is sent to the response or stored in an EL variable for later manipulation. To store the result of evaluating a fragment to an EL variable, you specify the var or varReader attributes. If var is specified, the container stores the result in an EL variable of type String with the name specified by var. If varReader is specified, the container stores the result in an EL variable of type java.io.Reader with the name specified by varReader. The Reader object can then be passed to a custom tag for further processing. A translation error occurs if both var and varReader are specified.

An optional scope attribute indicates the scope of the resulting variable. The possible values are page (default), request, session, or application. A translation error occurs if this attribute appears without specifying the var or varReader attribute.

Example 2 – DisplayProduct.tag

<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="normalPrice" fragment="true" %> <%@ attribute name="onSale" fragment="true" %> <%@ attribute name="anotherJSPFragment" fragment="true" %> <%@ variable name-given="name" %> <%@ variable name-given="price" %> <%@ variable name-given="origPrice" %> <%@ variable name-given="salePrice" %>

.....

Example 2– DisplayProduct.jsp -- page

Products (Same tag, alternate style)

<tags:displayProducts> <jsp:attribute name="normalPrice"> ${name} @ ${price} ea. </jsp:attribute> <jsp:attribute name="onSale"> ${name} @ ${salePrice} ea. (was: ${origPrice}) </jsp:attribute> </tags:displayProducts>

Example 2 output