Design and develop custom tags

This page discusses - Design and develop custom tags

Design and develop custom tags

Design and develop custom tags

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.

  1. Creating a tag handler class

    From the Java perspective:

    1. Select titan-web application.

    2. 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

      Creating a tag handler class
    3. Click Finish. The source file will open automatically.

    4. 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;
      	}
      }
      											
      											
    5. Save and close the file.

  2. Creating a tag library descriptor file

    The tag must be defined in a TLD file. To create the TLD file from the Web perspective:

    1. Select titan-web application.

    2. Select the Web module WEB-INF. 0

    3. Right-click the WEB-INF folder.

    4. Select New > Other > Simple > File. Click Next.

    5. Name the parent folder titan-web/WebContent/WEB-INF/tlds. 1

      Name the file titanTags.tld

      Creating a tag library descriptor file
    6. 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+) >
      
      											
      											
    7. Save and close the file. 2

  3. Using a custom tag library

    Before the CopyrightInfo tag can be used, the following taglib reference must be made:

    1. Place the following taglib definition on the first line of the JSP source file: 3

      
      <%@ taglib uri="/WEB-INF/tlds/titanTags.tld" prefix="tt" %>
      
      											
    2. Place copyright information tag in the code.

      											
      <tt:copyrightInfo />
      
      											
    3. Save and close the file.

    Visit http://java.boot.by  for the updates. 4

Tutorials

  1. Appendix A. Additional materials
  2. WSAD 5.0 Practicing for IBM Test 000-287 Mind Map
  3. Deploy enterprise applications into servers
  4. Chapter 6. Assemble enterprise applications and deploy them in IBM WebSphere Application Server
  5. Configure resource and security-role references
  6. Design and develop custom tags
  7. Chapter 4. Demonstrate understanding of database connectivity and messaging within IBM WebShpere Application Server
  8. Chapter 5. EJB transactions
  9. Design and develop message-driven EJBs
  10. Design and develop entity EJBs
  11. Validate operational parameters of application server to support the enterprise application
  12. Chapter 1. Design, build and test reusable enterprise components
  13. Access container and server services from enterprise components
  14. Part I. Exam Objectives
  15. Explain implications of resource management on application design and implementation
  16. Manage end-user state and understand performance tradeoffs of using HTTP sessions
  17. Chapter 7. Validate, tune and troubleshoot an application within an IBM WebSphere Application Server environment
  18. Implement mechanisms for efficient inter-component calls
  19. IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide
  20. Chapter 3. Develop clients that access the enterprise components
  21. Implement Java clients calling Web Services
  22. Configure JMS connection factories and destinations
  23. Design, develop and test JSPs
  24. Use JTA to control transaction demarcation
  25. Manipulate transactional behavior of EJBs using deployment descriptors
  26. Implement mechanisms which support loose coupling between clients and components
  27. Identify misbehaving application components
  28. Preface
  29. WSAD 5.0 Practicing for IBM Test 000-287
  30. Part II. Appendixes
  31. Interact with connection pools to obtain and release connections
  32. Describe the effects of a server failure on the application
  33. Test and debug enterprise components
  34. Chapter 2. Design, build and test web components
  35. Chapter 2. Design, build and test web components