Custom tag libraries
A custom tag library is a set of custom tags that invoke custom actions in a JavaServer Pages (JSP) file. Tag libraries reduce the task of embedding excessive amounts of Java code in JSP pages by moving the functionality provided by the tags into tag implementation classes.
Tag libraries are usually created by developers who are proficient in the Java programming language and can be used by Web designers who may not know Java, but would like to enhance their Web site by taking advantage of Java encoded tag libraries.
WebSphere Studio includes a library of custom tag libraries as part of the Sun Microsystems JSP 1.2 Specification making it easy for you to select and insert a particular tag from a tag library.
Tag libraries can be developed in WebSphere Studio. You can optionally add some of the Jakarta Project tag libraries directly from the Dynamic Web Project wizard. Several of the common tag libraries are available as Web project features.
NOTE: If you do not include these features when you create a Web project, you can add them from the project's Web Project Features properties page later.
Tag libraries:
Help separate presentation from implementation.
Are easy to maintain and reuse.
Simplify complex actions.
Provide Java-coded functions without the task of coding in Java.
Can dynamically generate page content and implement a controlled flow.
The web.xml file provides the link between the directive used in the application and the actual JAR file containing the classes that execute the function.
Creating a custom tag library
At some point, you may wish to create your own custom tag library. This example shows you the steps for creating a new tag library called copyrightInfo, which outputs the copyright information at the bottom of web page.
In order to use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior, the tag library descriptor file that maps the XML element names to the tag implementations, and the JSP file that uses the tag library.
Creating a tag handler class
From the Java perspective:
Select titan-web application.
Select File > New > Class. Type the following in the New Java Class window:
Package : com.titan.tags
Name : CopyrightInfo
Superclass : javax.servlet.jsp.tagext.TagSupport
Click Finish. The source file will open automatically.
Delete the automatic code and replace it with the following code:
package com.titan.tags; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * @author Mikalai Zaikin */ public class CopyrightInfo extends TagSupport { public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.print("<p>(c) Copyright Mikalai Zaikin 2005</p>"); } catch (IOException ioe) { System.out.println("Error in CopyrightInfo: " + ioe); } return (SKIP_BODY); } public int doEndTag() { return EVAL_PAGE; } }
Save and close the file.
Creating a tag library descriptor file
The tag must be defined in a TLD file. To create the TLD file from the Web perspective:
Select titan-web application.
Select the Web module WEB-INF. 0
Right-click the WEB-INF folder.
Select New > Other > Simple > File. Click Next.
Name the parent folder titan-web/WebContent/WEB-INF/tlds. 1
Name the file titanTags.tld
Click Finish. The source file will open automatically. Delete the automatic code and replace it with the following code:
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>titanTags</short-name> <description>Tag library for Titan application</description> <tag> <name>copyrightInfo</name> <tag-class>com.titan.tags.CopyrightInfo</tag-class> <body-content>empty</body-content> </tag> </taglib>
Here is a defintion for the taglib element for JSP 1.2:
<!ELEMENT taglib (tlib-version, jsp-version, short-name, uri?, display-name?, small-icon?, large-icon?, description?, validator?, listener*, tag+) >
Save and close the file. 2
Using a custom tag library
Before the CopyrightInfo tag can be used, the following taglib reference must be made:
Place the following taglib definition on the first line of the JSP source file: 3
<%@ taglib uri="/WEB-INF/tlds/titanTags.tld" prefix="tt" %>
Place copyright information tag in the code.
<tt:copyrightInfo />
Save and close the file.
Visit http://java.boot.by for the updates. 4