Home arrow Java & J2EE arrow Page 9 - Overview of Java Web Technologies, Part 2

Writing and Using Tags - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

TABLE OF CONTENTS:
  1. Overview of Java Web Technologies, Part 2
  2. Sun's Solution
  3. JSP and JavaBeans
  4. Calling a Bean from a JSP Page
  5. Accessing Bean Properties
  6. Custom Tags
  7. Developing and Using Custom Tag Libraries
  8. Writing a Tag Handler
  9. Writing and Using Tags
  10. Model 2 Architecture
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 9
March 03, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Writing a Tag Library Descriptor (TLD)

A TLD is an XML document that describes a tag library. A TLD is validated against a DTD file. Currently, the latest version of the DTD is 1.2. A TLD must begin with the following header:

 
<?xml version="1.0" encoding="ISO-8859-1" ? > 
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag 
Library 1.2//EN" 

"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"

The root element of a TLD file is <taglib>. The subelements for the taglib element are as follows:

 
<!ELEMENT taglib (tlib-versionjsp-versionshort-nameuri?, 
display
-name?, small-icon?, large-icon?, description?, validator?, 
listener
*, tag+) > 

A taglib element must have the following elements:

  • The tlib-version element specifies the version of the tag library implementation.
  • The jsp-version element defines the JSP version that the tag library can work with.
  • The short-name element encloses a unique name for the tag library.
  • The tag element specifies a custom tag in the library. Its subelements are as follows:

 
<!ELEMENT tag (nametag-class, tei-class?, body-content?, 
display
-name?, small-icon?, large-icon?, description?, variable*, 
attribute
*, example?) > 

The other subelements are optional. You can read the description of each element in the DTD file downloadable from http://java.sun.com/dtd/web-jsptaglibarary_1_2.dtd.

Using a Custom Tag in a JSP Page

To use a custom tag in a JSP page, you need to be familiar with the taglib directive in JSP. A taglib directive has the following syntax:

 
<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %> 

The uri attribute specifies an absolute or relative URI that uniquely identifies the TLD associated with this prefix. The prefix attribute defines a string that will become the prefix to distinguish a custom action.

With a taglib directive, you can use a custom tag of the following format for a custom tag that does not have a content body:

 
<prefix:tagName/> 

Or, you can use the following format for a custom tag that has a content body:


<prefix:tagName>body</prefix:tagName

You can pass attributes to the tag handler by specifying the attributes in the custom tag, each with the following format:

 
attributeName="attributeValue" 

The following example is a custom tag whose prefix is m and whose name is myTag. The tag has two attributes: number, with a value of 12, and power, with a value of 13.

 
<m:myTag number="12" power="13"/> 

Note that an attribute value must be enclosed in quotation marks.



 
 
>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: