JSP and Tag Libraries

Download Report

Transcript JSP and Tag Libraries

Java for the WWW
Tag Libraries
• What are they?
– Custom libraries
• Custom tags – see examples in following slides
– Standard Tag Library (JSTL) – recommend that you read the
following:
• http://today.java.net/pub/a/today/2003/10/07/jstl1.html
• http://today.java.net/pub/a/today/2003/11/27/jstl2.html
• Purpose and benefits for JSP web development
– Lot of development and includes the AJAX technology but
this is outside the scope of this module
• For further reading on Tag libraries – here is nice
tutorial to back up the following notes <link>
November 2012
[email protected]
Slide 1
Java for the WWW
Custom tags
• Custom tags are dynamic page elements.
• This means they can provide functionality to a
web page using a syntax similar to normal
HTML tags.
• The functionality is up to you…
• They can provide an alternative (or an
additional) mechanism to JavaBeans for
dynamic scripting.
March 2009
[email protected]
Slide 2
Java for the WWW
What do you need?
• There are two components to a tag:
– A Java tag handler class that implements the tag’s
functionality.
– A Tag Library Descriptor (TLD) in the form of an
XML file describing the tag.
• By including this tag on a JSP page you have
access to its functionality.
March 2009
[email protected]
Slide 3
Java for the WWW
Java handler class
• Yes, you have to code the functionality in
Java.
• So why bother? Why not just create a
JavaBean instead?
– For standard web page scripters, tags are more
familiar and provide a simpler mechanism.
– For web application developers, JavaBeans
integrate more fully with the full application
environment so are more powerful.
• Your choice….
March 2009
[email protected]
Slide 4
Java for the WWW
Making the handler
• You Java class needs to extend the TagSupport class.
• This class is only available in the Enterprise version of
the Java Development Kit (from version 1.3.1).
• Or, the Java Web Services Developer Pack.
• However, you just need the lib/j2ee.jap file (in the case
of J2EE) or the lib/servlet.jar for the JWSDP.
• You can then extract the javax.servlet.jsp.tagext class
from the .jar file (using WinZip etc.) and put it into
folder containing your tag classes – see screen grab
on next powerpoint
March 2009
[email protected]
Slide 5
Java for the WWW
March 2009
[email protected]
Slide 6
Java for the WWW
Making the handler
• Once you have your custom tag Java code,
you need to compile it and put the resultant
.class file in your WEB-INF/classes folder.
• For the examples, this would go in:
– WEB-INF/classes/mytags
March 2009
[email protected]
Slide 7
Java for the WWW
Example handler
March 2009
[email protected]
Slide 8
Java for the WWW
Tag Library Descriptor
• A TLD is a (fairly) simple XML file that
describes a set of custom tags.
• You can put as many or as few custom tags in
a TLD and more than one TLD can be used in
a single project.
• In principle, you could create many useful
tags and zip them into one .jar file for easy reusability.
March 2009
[email protected]
Slide 9
Java for the WWW
Example Tag Descriptor
[email protected]
Slide 10
Java for the WWW
Using your custom tag
• To use your custom tag, you need a JSP
page.
• You need to tell your JSP where to find your
tag library so you use a page directive:
<%@ taglib uri=“WEB-INF/nameofdescriptor.tld”
prefix=“something” %>
• Anywhere in your JSP page, you can then
use your custom tag(s):
March 2009
[email protected]
Slide 11
Java for the WWW
Example with a JSP
March 2009
[email protected]
Slide 12
Java for the WWW
The use of tag libraries is all about...
• Re-usability
• Defining things once and making them available for
use in more than one place
• Simplification
• Bespoke JSP work
• Or using existing tag libraries available for use - See
the following
Jsptags.com
Jspin
Jakata Apache Taglibs project
March 2009
[email protected]
Slide 14