Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Design and develop custom tags

This page discusses - Design and develop custom tags

Ads

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

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.

    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.

      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.

  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:

      
      <%@ 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.

Advertisements

Share on Google+Share on Google+

Design and develop custom tags

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Design and develop custom tags  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads