8.1 Custom Tags Overview

This section provides a brief overview of JSP custom tags and how they can add value to an application. They are explicitly tied to the JavaServer Pages technology and therefore are used only when building web applications based on JSP, such as those built using the Struts framework.

8.1.1 What Is a Tag?

Before we talk specifically about JSP tags, it's important that you understand what a tag is in general terms. Keep in mind that this section refers to tags in general, not JSP custom tags or Struts tags. We'll discuss those shortly.

If you're familiar with HTML, you already should have a good understanding of the concept of a tag. There are two basic types of tags:

  • Bodyless tags

  • Tags with a body

Bodyless tags are tags that specify attributes but contain no content. They have the syntax:

<tagName attributeName="someValue" attributeName2="someValue2"/>

Bodyless tags are most often used to perform simple actions such as rendering HTML fields or displaying images. An example of a bodyless tag is:

<img src="/books/2/63/1/html/2/images/struts-power.gif"/>

Tags can define certain predefined attributes, which supply information to the tag and can affect how the tag performs its duties. In the HTML img tag, for example, the src attribute supplies the tag with the path to a graphical image that will be rendered by the tag. The tag is generic it knows nothing specific about the image ahead of time. It's designed to receive an image path using the src attribute and display that image at runtime.

Tags with a body have a start tag and a matching end tag, with some content between them. The syntax looks like:

<tagName attributeName="someValue" attributeName2="someValue2">   <!-- The Tag body is between the start and end tags --> </tagName>

Tags with a body are used to perform operations on the body content, such as iterating over a collection, formatting HTML output, and so on. Here's another example from HTML:

<html>   <!-- The HTML body inside the start and end HTML tags --> </html>

The end tag must always begin with a / character.

8.1.2 What Is a JSP Custom Tag?

When parsing an HTML file, the browser determines how to process and handle the tags contained within the file based on a set of standards. The purpose of JSP custom tags is to give the developer the ability to extend the set of tags that can be used inside a JSP page. With ordinary HTML tags, the browser contains the logic to process the tag and render the output. With JSP custom tags, the functionality exists in a special Java class called the tag handler.

The tag handler is a Java class that carries out the specific behavior of the tag. It implements one of several custom tag interfaces, depending on the type of tag that you need to develop. The handler class has access to all of the JSP resources, such as the PageContext object and the request, response, and session objects. The tag also is populated with the attribute information, so it can customize its behavior based on the attribute values. Advantages of using custom tags

There are many benefits of using custom tags instead of scriptlets and Java code in your JSP pages:

  • Tags are reusable, which saves precious development and testing time.

  • Tags can be customized using attributes, either statically or dynamically.

  • Tags have access to all of the objects available to the JSP page, including request, response, and output variables.

  • Tags can be nested, which allows for more complex interactions within a JSP page.

  • Tags simplify the readability of a JSP page.

In general, using JSP tags helps to further the concept of reuse, as the behavior is implemented in a single location, the tag handler; it's not replicated throughout multiple JSP pages.

8.1.3 What Is a Tag Library?

A tag library is a set of JSP custom tags grouped together from a packaging perspective. Although it's not a requirement, the tags within a tag library should solve a similar type of problem. Because web applications can include multiple tag libraries, there's no need to place all of the tags into a single library.

You can see an example of how tags can be grouped logically by looking at the Jakarta Taglibs project at http://jakarta.apache.org/taglibs. Tag libraries are available for rendering dates and times, manipulating strings, and many other purposes. Notice that each tag library is focused on a single concept or task. The Jakarta Taglibs project will be discussed later in this chapter. Tag library components

A tag library is made up of the following components:

  • Tag handler

  • Tag library descriptor file

  • The application deployment descriptor (web.xml) file

  • The tag library declaration in the JSP page Tag handler

You've already been introduced to the tag handler. This is where the implementation of the tag is located. It's a Java class that gets invoked at runtime and that performs some predefined behavior. The TLD file

The tag library descriptor (TLD) file is an XML file that contains meta-information about the tags within a library. Information such as the tag name, the attributes that are required, and the tag handler class name are all contained in this file and read in by the JSP container. The web.xml file

We discussed web application deployment descriptors in Chapter 3. Within this descriptor, you must define what tag libraries are being used in the web application and which TLD files should be used to describe each tag library. The JSP page

Obviously, the JSP page is a key component. It contains the include directives for one or more tag libraries, as well as the needed calls to the tag libraries within the JSP page. There is essentially no limit to how many tag libraries or tag references you can have in a JSP page.

There have been some reports of sluggishness in JSP containers when the number of tags in a single JSP page approaches 40-50. Vendors have a lot of freedom in terms of how they initialize and process custom tags; some are better than others. If this becomes a problem for your implementation, you might try to redesign the page to reduce the number of tags or combine some of the tags into a single handler. If that doesn't solve your problem, run your tags in a different container and evaluate the performance.

Attempting to cover all aspects of JSP custom tags is beyond the scope of this book. In fact, there are entire books written on the subject. A good source of information on custom tags and their use in applications is Hans Bergsten's JavaServer Pages (O'Reilly).

Programming Jakarta Struts
Programming Jakarta Struts, 2nd Edition
ISBN: 0596006519
EAN: 2147483647
Year: 2003
Pages: 180

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net