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

Slides:



Advertisements
Similar presentations
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Advertisements

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Apache Struts Technology
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
JavaServerPages Some examples. About JSP Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
JSP Tag Extensions And Java Bean. JSP Tag Extensions Tag extensions look like HTML (or rather, XML) tags embedded in a JSP page. They have a special meaning.
DT211/3 Internet Application Development
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
JSP Java Server Pages Reference:
Apache Struts Technology A MVC Framework for Java Web Applications.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
JSP Standard Tag Library
Java for the WWW November 2012Slide Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
© 2006 IBM Corporation IBM WebSphere Portlet Factory Architecture.
Middleware Technology JavaServer Pages (JSP)
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
Chapter 7 Using Custom Tag Libraries and the JSP Standard Tag Library.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
JSTL The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
Chapter 2 Java Struct 2. Content Basic MVC Architecture Struts 2 Overview Struts 2 - Architecture.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
Apache Struts Technology A MVC Framework for Java Web Applications.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
CS3220 Web and Internet Programming Custom Tag Library
JSP: Actions elements and JSTL
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
Presentation transcript:

® IBM Software Group © 2007 IBM Corporation JSP Custom Tags

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

7 JSP Page Without Custom Tags <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> page language="java" contentType="text/html; charset=ISO " pageEncoding="ISO "%> 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 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 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 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 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 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 Tag Examples  Basic  With attributes  With attributes and a body "Hello world."  Defining scripting variables

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 General Format of the TLD (1 of 2) Defines the date tag <taglib version="2.0" xmlns=" xmlns:xsi=" xsi:schemaLocation=" 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 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 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: java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd

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 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 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 Processing Tags with Attributes: How It Works This is text to be transformed. 1)Initialize and set attributes (setMode()) 2) Call doTag() method

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 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 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 The Taglib Descriptor <taglib version="2.0" xmlns=" xmlns:xsi=" xsi:schemaLocation=" "> Tag Library for Library System 1.0 ilib transform com.ibm.library.tag.TransformTag scriptless mode true

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

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