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 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. 8.1.2.1 Advantages of using custom tagsThere are many benefits of using custom tags instead of scriptlets and Java code in your JSP pages:
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. 8.1.3.1 Tag library componentsA tag library is made up of the following components:
8.1.3.2 Tag handlerYou'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. 8.1.3.3 The TLD fileThe 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. 8.1.3.4 The web.xml fileWe 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. 8.1.3.5 The JSP pageObviously, 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.
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). |