Docsity

JSP Custom Tags: Declaration, Syntax, and Building, Slides of Java Programming

Toggle sidebar
bg1
Declaring a tag library
`Include taglib directive before tags are used
`Syntax
`<%@ taglib prefix="myprefix" uri=โ€myuriโ€ %>
`prefix: identifies the tag library
`uri: uniquely identifies the tag library descriptor (TLD)
directly or indirectly
๎˜ƒ
๎˜ƒ
You๎˜ƒdeclare๎˜ƒthat๎˜ƒa๎˜ƒJSP๎˜ƒpage๎˜ƒwill๎˜ƒuse๎˜ƒtags๎˜ƒdefined๎˜ƒin๎˜ƒa๎˜ƒtag๎˜ƒlibrary๎˜ƒby๎˜ƒincluding๎˜ƒa๎˜ƒtaglib๎˜ƒdirective๎˜ƒin๎˜ƒthe๎˜ƒpage๎˜ƒ
before๎˜ƒany๎˜ƒcustom๎˜ƒtag๎˜ƒfrom๎˜ƒthat๎˜ƒtag๎˜ƒlibrary๎˜ƒis๎˜ƒused.๎˜ƒIf๎˜ƒyou๎˜ƒforget๎˜ƒto๎˜ƒinclude๎˜ƒthe๎˜ƒtaglib๎˜ƒdirective๎˜ƒfor๎˜ƒa๎˜ƒtag๎˜ƒ
library๎˜ƒin๎˜ƒa๎˜ƒJSP๎˜ƒpage,๎˜ƒthe๎˜ƒJSP๎˜ƒcompiler๎˜ƒwill๎˜ƒtreat๎˜ƒany๎˜ƒinvocation๎˜ƒof๎˜ƒa๎˜ƒcustom๎˜ƒtag๎˜ƒfrom๎˜ƒthat๎˜ƒlibrary๎˜ƒas๎˜ƒ
template๎˜ƒdata,๎˜ƒand๎˜ƒsimply๎˜ƒinsert๎˜ƒthe๎˜ƒtext๎˜ƒof๎˜ƒthe๎˜ƒcustom๎˜ƒtag๎˜ƒcall๎˜ƒinto๎˜ƒthe๎˜ƒresponse.๎˜ƒ
๎˜ƒ
The๎˜ƒsyntax๎˜ƒof๎˜ƒtaglib๎˜ƒdirective๎˜ƒis๎˜ƒas๎˜ƒfollowing:๎˜ƒ
๎˜ƒ
<%@๎˜ƒtaglib๎˜ƒprefix="tt"๎˜ƒuri=URI๎˜ƒ%>๎˜ƒ๎˜ƒ
๎˜ƒ
The๎˜ƒprefix๎˜ƒattribute๎˜ƒdefines๎˜ƒthe๎˜ƒprefix๎˜ƒthat๎˜ƒdistinguishes๎˜ƒtags๎˜ƒdefined๎˜ƒby๎˜ƒa๎˜ƒgiven๎˜ƒtag๎˜ƒlibrary๎˜ƒfrom๎˜ƒthose๎˜ƒ
provided๎˜ƒby๎˜ƒother๎˜ƒtag๎˜ƒlibraries.๎˜ƒ๎˜ƒThe๎˜ƒtagdir๎˜ƒattribute๎˜ƒspecifies๎˜ƒthe๎˜ƒlocation๎˜ƒof๎˜ƒtag๎˜ƒfiles.๎˜ƒAnd๎˜ƒuri๎˜ƒattribute๎˜ƒ
uniquely๎˜ƒidentifies๎˜ƒthe๎˜ƒtag๎˜ƒlibrary๎˜ƒdescriptor๎˜ƒ(TLD)๎˜ƒeither๎˜ƒdirectly๎˜ƒor๎˜ƒindirectly.๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ
๎˜ƒ๎˜ƒ
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 Custom Tags: Declaration, Syntax, and Building and more Slides Java Programming in PDF only on Docsity!

Declaring a tag library

Include taglib directive before tags are used Syntax &lt;%@ taglib prefix="myprefix" uri=โ€myuriโ€ %&gt; prefix: identifies the tag library ` uri: uniquely identifies the tag library descriptor (TLD) directly or indirectly

You declare that a JSP page will use tags defined in a tag library by including a taglib directive in the page before any custom tag from that tag library is used. If you forget to include the taglib directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as template data, and simply insert the text of the custom tag call into the response.

The syntax of taglib directive is as following:

<%@ taglib prefix="tt" uri=URI %>

The prefix attribute defines the prefix that distinguishes tags defined by a given tag library from those provided by other tag libraries. The tagdir attribute specifies the location of tag files. And uri attribute uniquely identifies the tag library descriptor (TLD) either directly or indirectly.

Custom Tag Syntax in JSP page

or

body

prefix: distinguishes tag library tag: identifies a tag (within the tag library) attr1 ... attrN: modify the behavior of the tag

Custom tags have the syntax

or

body

where prefix distinguishes tags for a library, tag is the tag identifier, and attr1 ... attrN are attributes that modify the behavior of the tag.

Example: Accessing SimpleExample.jsp

This picture shows when the JSP page in the previous slide is accessed.

Source Directory Structure

Use the following source directory structure (as in J2EE 1.4 Tutorial's web/iterator directory) /j2eetutorial14/examples/web ` iterator

build.xml (ant build file)

src (dir where *.java files with proper package structure reside)

web (dir)

WEB-INF (dir)

*.tld

tags (dir - needed for JSP 2.0 tag files)

*.jsp

First, let's see what would be a desired source directory structure. Of course, how you structure your development directory structure is dependent on individual taste, a recommended approach is to have separate the source tree from the build/deployment tree. And that is what Java WSDP tutorial code is structured and that is what we will use here as well.

Java WSDP 1.2 has โ€œiteratorโ€ custom tag and it comes with ant build.xml file. So for our custom tag development, we will have a source directory structure mentioned in the slide. That is, build.xml is in the top directory. And we will have src directory which contains *.java source files. And the web directory has WEBโ€INF subdirectory which will contain the TLD file and JSP files will be under web directory.

Build Directory Structure

(under build or dist directory)

Use the following build directory structure (as in J2EE 1.4 Tutorial's web/iterator directory) /j2eetutorial14/examples/web ` iterator

build (unpacked *.war file)

dist (packed *.war file)

OK, now let's see what would be the build directory structure. We will have โ€œbuildโ€ directory and โ€œdistโ€ directory. The build directory will reflect the unpacked form of the custom tag library while the dist directory contains the *.jar file.

Expression Language

The first major enhancement in JSP 2.0 is integration of Expression Language (EL) as a builtโ€in feature of JSP. Expression Language has been around as part of Java Standard Tag Library (JSTL) 1.0. Now due to its huge success, EL is now part of JSP itself. That is, Servlet 2.4 and JSP 2.0 compatible container should be able to handle EL expressions as native JSP syntax.

Expression Language

Based on โ€œSPELโ€ from JSTL 1. Simplest Possible Expression Language Let you access the property values of a JavaBean in a simpler syntax Example: ${item.price} Recognized by JSP container in: Template text Attributes of any standard or custom action Support for custom EL functions: Extensible via tag libraries Example: ${fn:allCaps(lastName)} ` JSTL 1.1 provides 16 standard EL functions

Expression language is based on โ€œSimplest Possible Expression Languageโ€ from JSTL 1.0. It basically let you access the property values of a JavaBean in a simpler syntax. For example, โ€œpriceโ€ property value of โ€œitemโ€ object can be displayed by using ${item.price} syntax. It also let you perform arithmetic operations, comparisons. You can also access the implicit objects such as form input parameters from the HTML client.

The JSP expression language allows you to define a function that can be invoked in an expression. If you are familiar with string manipulation code, the functions serve somewhat similar purpose. The difference is that under JSP 2.0, these functions can be invoked using EL syntax.

Functions are defined using the same mechanisms as custom tags. For example, if there is a function, for example, allCaps() method that is defined inside tag handler, you can call it through EL syntax, for example, โ€œfnโ€ prefix with the method name and parameter.

Integrated Expression Language Example

` Using scriptlets:

<% Map m = (Map)pageContext.getAttribute("states" ); State s = ((State)m.get( "NY" )); if( s != null ) { %> <%= s.getCapitol() %> <% } %>

` Equivalent, using an EL expression:

${states["NY"].capitol} ${states["NY"].capitol.name}

This is another example expression language, this time with a Map collection object in which multiple keyโ€object pairs, in this case, state and its capital city pairs, are stored.

Again, if you want to get the capital city of a particular state from states map object, you provide the key value with a bracket and the access the capital property.

JSP 1.2 Syntax With Scriptlets

<%-- Output Shopping Cart --%> <%@ page import="com.acme.util.*" %> <%@ taglib prefix="util" uri="http://mytaglib" %>

<% for( int i = 0; i < cart.size(); i++ ) { CartItem item=(CartItem)cart.get(i); %>

<%= item.getName() %> <%= item.getPrice() %>

<% } %>

This slide shows a JSP page in which the logic of displaying lineitems of a shopping cart is written using the JSP 1.2 syntax with scriptlets. And as you can see, the page is rather hard to build and read. Now let's see how this page can be simplified using JSP 2.0 syntax in the following slide.

JSP 2.0 Improved XML Syntax

**

${item.name} ${item.price}

**

This slide shows the same JSP 2.0 based page but using XML syntax even for the directives. One reason you might want to build your JSP page as XML document is to be able to use XML schema for validation and to allow XML tools to be used for building and manipulating the JSP page.

Simple Tag Handlers &

Extensions

Now let's talk about simple tag handlers and extensions, which is one of the significant enhancements in JSP 2.0.

Simple Tag Handlers

Simpler to use than classic tag handlers Implement SimpleTag interface Usually extend SimpleTagSupport class doTag() of SimpleTag interface gets invoked when the end element of the tag is encountered ` You want to override this method

Why do you want to use simple tag handlers as opposed to classic tag handlers? Simple tag handlers are much simpler to use than the classic tag handlers.

The classes and interfaces used to implement simple tag handlers are contained in the javax.servlet.jsp.tagext package. Simple tag handlers implement the SimpleTag interface. Interfaces can be used to take an existing Java object and make it a tag handler. For most newly created handlers, you would use the SimpleTagSupport classes as a base class.

The heart of a simple tag handler is a single methodโ€โ€doTag() methodโ€โ€which gets invoked when the end element of the tag is encountered. Note that the default implementation of the doTag method() of SimpleTagSupport does nothing. And you want to override this doTag() method most of the time.

How is a Simple Tag Handler called

from container?

ATag t = new ATag(); t.setJSPContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); ... t.setJspBody(new JspFragment(...)) t.doTag();

The SimpleTag interface defines the basic protocol between a simple tag handler and servlet container. The servlet container invokes the setJspContext(), setParent(), and attribute setting methods() before setting JspFragment and then calling doTag() method

ATag t = new ATag(); t.setJSPContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); ... t.setJspBody(new JspFragment(...))รพ t.doTag();