Presentation is loading. Please wait.

Presentation is loading. Please wait.

® IBM Software Group © 2007 IBM Corporation JSP Custom Tags 4.1.0.3.

Similar presentations


Presentation on theme: "® IBM Software Group © 2007 IBM Corporation JSP Custom Tags 4.1.0.3."— Presentation transcript:

1 ® IBM Software Group © 2007 IBM Corporation JSP Custom Tags 4.1.0.3

2 2 After completing this unit, you should be able to:  Describe the advantages of using JSP custom tags  List the major steps in developing and using JSP custom tags  Develop basic tag handler classes to implement JSP custom tags  Create and modify taglib descriptor files  Package JSP taglib implementation classes and taglib descriptor files  Understand the uses of the JSTL  Name some of the tags included in the JSTL and their purposes After completing this unit, you should be able to:  Describe the advantages of using JSP custom tags  List the major steps in developing and using JSP custom tags  Develop basic tag handler classes to implement JSP custom tags  Create and modify taglib descriptor files  Package JSP taglib implementation classes and taglib descriptor files  Understand the uses of the JSTL  Name some of the tags included in the JSTL and their purposes Unit objectives

3 3 JSP Custom Tags Overview  Nine standard actions must be provided by any compliant JSP implementation:  useBean, setProperty, getProperty  include, forward  plug-in, params, param, fallback  Custom tags allow developers to create additional actions beyond the standard set  Custom actions are invoked via custom tags in a JSP page  Tag libraries are collections of custom tags  Support for JSP custom tags is required by the JSP specification

4 4 Why Use JSP Custom Tags?  Role-based development  Model classes (business objects and data storage layers) are developed by Java and EJB developers  Controller classes (servlets) are developed by Java developers  View-based JSP pages are developed by HTML developers  Different roles:  Use different tools  Have different skills  Best Practice  MVC design is well established  Use the right tools for the right jobs

5 5 Steps to Create and Use a Custom Tag Library  To develop a tag, you need to:  Design your tags and attributes  Declare the tag in a tag library descriptor (TLD)  Develop a tag handler class  Develop helper classes for the tag (if needed)  To use a custom tag, the JSP needs to:  Include the tag library using the taglib directive  Code the custom tag with any needed attributes  Test your tags class 1 class 2 helper class TLD JSP Page

6 6 Tag Usage Example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 transitional//EN"> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1“ %> Date Demo Date Demo Fully formatted date:

7 7 JSP Page Without Custom Tags <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Date Demo Date Demo Fully formatted date: <% java.util.Locale locale = pageContext.getRequest().getLocale(); java.text.DateFormat fmt = java.text.DateFormat.getDateInstance (java.text.DateFormat.FULL,locale); String date = fmt.format(new java.util.Date()); %>

8 8 Using Custom Tags with Application Developer  Page Designer has different ways of selecting a tag for inclusion with JSP 1.Select JSP->Insert Custom 2.Drag Custom from JSP Tags drawer in Palette  Select desired tag from Insert Custom Tag dialog 12

9 9 JSP Standard Tag Library (JSTL)  Encapsulates as tags core functionality of many Web applications  Supports tasks such as:  Flow (iteration and conditionals)  Manipulation of XML documents  Internationalization tags  SQL tags  J2EE 1.4 includes both JSP and JSTL  JSTL taglibs included with Rational Application Developer

10 10 Sample JSTL Tags  Set a variable in a specific scope to a value   Display a value, or an alternative if the first value is null   Example: Hello !  Conditional execution , and Welcome, member! Welcome, guest!

11 11 forEach Tag  Provides flexible iteration through a set of items  Targets include:  Collections, Maps, Iterators, Enumerations  Arrays  Comma-separated values  SQL ResultSets  Example: ${cust.name} ${cust.addr}

12 12 Anatomy of a Tag Instructions for logging in to the system: (1)Enter your Patron identifier in ID field (2)Enter assigned password in PW field (3)Click on LOGIN button Start tag Body (optional) End tag Attribute (optional) Element

13 13 Tag Examples  Basic  With attributes  With attributes and a body "Hello world."  Defining scripting variables

14 14 Describing Tags to the JSP Container  Done with the taglib descriptor (TLD)  XML file  Describes the tag library  Files use the.tld extension  Defines the syntax of the tags (actions)  Defines the attributes (if any) for the tags  Specifies if the attribute is optional or required  Specifies the Java class that implements the tag  Specifies if the tag allows or uses a body  Used by the JSP container to validate the JSP at compile time

15 15 General Format of the TLD (1 of 2) Defines the date tag <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web- jsptaglibrary_2_0.xsd"> date com.ibm.library.tag.FormattedDate empty Display current date Tag Library from IBM Library System 1.0 ilib Required Info about TLD Tag Info

16 16 General Format of the TLD (2 of 2) date2 com.ibm.library.tag.FormatDate2 JSP format true Action name Tag handler class implementation How to process the body Attribute name (multiple allowed) Optional (false) or mandatory (true)

17 17 Location of TLD File  Resides in the META-INF directory or subdirectory when deployed inside a JAR file  Resides in the WEB-INF directory or some subdirectory when deployed directly into a Web application  XML Schema is located at URL: http:// java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd

18 18 JSP Taglib Directive  Taglib directive tells your JSP the prefix to be used for a specific JSP tag library … date test … … Taglib directive Taglib usage Location of TLD Prefix for this JSP

19 19 Tag Handler Base Classes  Tag handlers must implement specific interfaces or extend specific classes, and must override key methods  These classes all reside in javax.servlet.jsp.tagext  JSP 2.0 introduced SimpleTag “classic” tags

20 20 Example Tag  The tag allows page developers to transform the contained text in two ways:  Convert it to upper case  Hide it  The tag has a required attribute mode with the following values:  upper  hide  The value of the attribute can be taken from a runtime expression This is text to be transformed.

21 21 Processing Tags with Attributes: How It Works This is text to be transformed. 1)Initialize and set attributes (setMode()) 2) Call doTag() method

22 22 What Needs to Be Done?  Create the TransformTag class  Update the TLD for the new date tag  Use the new tag in your JSPs handler class TLD JSP Page

23 23 The TransformTag Class package com.ibm.library.tag; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class TransformTag extends SimpleTagSupport { String mode = ""; public void setMode(String mode) { this.mode = mode.toUpperCase(); } // class continues on next page

24 24 The doTag() Method public void doTag() throws JspException, IOException { JspFragment body = getJspBody(); StringWriter oldbody = new StringWriter(); String newbody = null; body.invoke(oldbody); if (mode.equals("UPPER")) { newbody = oldbody.toString().toUpperCase(); } else if (mode.equals("HIDE")) { newbody = ""; } else { newbody = oldbody.toString(); } JspWriter out = getJspContext().getOut(); out.write(newbody); }

25 25 The Taglib Descriptor <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "> Tag Library for Library System 1.0 ilib transform com.ibm.library.tag.TransformTag scriptless mode true

26 26 Using the Tag <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> transformDemo.jsp Demonstrate <transform> tag This is text to be transformed This text is not to be transformed

27 27 Packaging  To facilitate reuse, the tag handler classes can be packaged together  Place the class files in a JAR  Import the TLD into /WEB-INF/tld  Import the JAR into /WEB-INF/lib  An additional option is to package the TLD with the class files JAR  Application Developer provides support for JSP tag library resource references  Web Deployment Descriptor editor  Variables tab  Allows URI to be specified to reference the TLD

28 28 Checkpoint 1.What are some of the advantages of JSP custom tags? 2.What are the major steps that must be performed during JSP custom tag development? 3.How are attributes’ values processed in a tag handler class? 4.What method of the SimpleTag interface does the main work of processing a tag? 5.What is the purpose of the JSP taglib directive?

29 29 Checkpoint solutions 1.Advantages of custom tags include: 2.Make JSPs easier to develop, test, and maintain 3.Web developer can focus on presentation (role-based developmental 4.Presentation logic is reusable 5.The major steps in JSP custom tag development are: 6.Design tags and attributes 7.Write tag handler class 8.Construct or modify TLD 9.Test in a JSP 10.Attribute values are processed in a tag handler class through JavaBean-like setter methods. 11.doTag() 12.The taglib directive describes the location of the TLD and designates the tag prefix.

30 30 Having completed this unit, you should be able to:  Describe the advantages of using JSP custom tags  List the major steps in developing and using JSP custom tags  Develop basic tag handler classes to implement JSP custom tags  Create and modify taglib descriptor files  Package JSP taglib implementation classes and taglib descriptor files  Understand the uses of the JSTL  Name some of the tags included in the JSTL and their purposes Having completed this unit, you should be able to:  Describe the advantages of using JSP custom tags  List the major steps in developing and using JSP custom tags  Develop basic tag handler classes to implement JSP custom tags  Create and modify taglib descriptor files  Package JSP taglib implementation classes and taglib descriptor files  Understand the uses of the JSTL  Name some of the tags included in the JSTL and their purposes Unit summary


Download ppt "® IBM Software Group © 2007 IBM Corporation JSP Custom Tags 4.1.0.3."

Similar presentations


Ads by Google