Contents [0/14] |
Overview of Today's Class [1/14] |
Creating Tags [2/14] |
Implementing a Tag Handler [3/14] |
The SimpleTag Interface [4/14] |
The Tag Interface [5/14] |
The IterationTag Interface [6/14] |
The BodyTag Interface [7/14] |
The TryCatchFinally Interface [8/14] |
The Tag Library Descriptor [9/14] |
Deploying Tag Libraries [10/14] |
Using Tag Libraries [11/14] |
JSTL [12/14] |
JSTL tags [13/14] |
Homework [14/14] |
Overview of Today's Class [1/14] |
Tag Libraries - implementing
Tag Files - implementing
JSTL - Intro
Filters
Creating Tags [2/14] |
A custom tag consists of a tag handler and a tag library descriptor
Types:
Tag, BodyTag
or IterationTag
interfaces.
SimpleTag
or
tag extensions created using only JSP syntax
instead of Java
To use the tag:
A custom tag must implement the javax.servlet.jsp.tagext.Tag
or javax.servlet.jsp.tagext.SimpleTag
interface (which now extends javax.servlet.jsp.tagext.JspTag
for organizational purposes
This is usually done by extending a base utility class
javax.servlet.jsp.tagext.SimpleTag implements JspTag
javax.servlet.jsp.tagext.TagSupport implements IterationTag, Tag
javax.servlet.jsp.tagext.BodyTagSupport implements BodyTag, IterationTag, Tag
IterationTag extends Tag
BodyTag extends IterationTag
The Tag
interface supplies doStartTag()
and doEndTag()
BodyTag
supplies doInitBody(), setBodyContent()
IterationTag
supplies doAfterBody()
TryCatchFinally
supplies doCatch(), doFinally()
and helps tags manage resources better
Implementing a Tag Handler [3/14] |
To implement a tag handler:
doTag()
-- for Simple TagsdoStartTag()
doEndTag()
doInitBody()
doAfterBody()
The SimpleTag Interface [4/14] |
doTag()
- Called by the container to invoke this tag,
and is called once and only once. The lifecycle for SimpleTag is
simpler than Tag, IterationTag,
or BodyTag.
Lifecycle:
setJspContext()
and setParent()
methods are called by the container. The setParent()
method is only called if the element is nested within another
tag invocation.
setJspBody()
method is called
by the container to set the body of this tag, as a JspFragment.
If the action element is empty in the page, this method is not
called at all.
doTag()
method is called by the container. All
tag logic, iteration, body evaluations, etc. occur in this method.
doTag()
method returns and all variables are synchronized.
If you need to create a SimpleTag
, but use it as a
classic tag, you can use a TagApapter
to wrap the
SimpleTag
and then the SimpleTag can be used where a classic
Tag
is expected.
The Tag Interface [5/14] |
doStartTag()
doEndTag()
Lifecycle:
The IterationTag Interface [6/14] |
doAfterBody()
Lifecycle:
The BodyTag Interface [7/14] |
doStartTag()
returns SKIP_BODY
doStartTag()
returns EVAL_BODY_INCLUDE
doAfterBody()
and doEndTag()
are invokedsetBodyContent(BodyContent b)
doStartTag()
returns EVAL_BODY_INCLUDEdoInitBody()
setBodyContent()
Lifecycle:
The TryCatchFinally Interface [8/14] |
An auxiliary interface for tags that want more control over resources
doCatch()
Throwable
occurs while evaluating
the body in
Tag.doStartTag()
Tag.doEndTag()
IterationTag.doAfterBody()
BodyTag.doInitBody()
doFinally
- invoked in all cases after
doEndTag()
for any class implementing Tag, IterationTag,
or BodyTag
The Tag Library Descriptor [9/14] |
In order for your tag library to be processed properly by the Servlet container, you must supply a configuration file known as a tag library descriptor. This basically servers the purpose of web.xml for a web application, it is information that the container uses to manage the libraries at runtime
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd for JSP 2.0
http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd for JSP 1.2
http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd for JSP 1.1>
TLD structure:
taglib
- requires tlib-version, jsp-version,
short-name, and tags. Optional are uri, description, validator
(a TagLibraryValidator
), and listeners.
tag
- requires name and tag class, optional
are body-content (JSP/empty, or tagdependent),
variables entries (scripting variable names),
attributes, tei (TagExtraInfo) classes, and description
attribute
- requires name,
required (whether it is required, false by default),
rtexprvalue (whether it is a runtime JSP expression),
type (must be fully qualified)
Examples:
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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" version="2.0"> <tlib-version>2.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>se452</short-name> <uri>/se452</uri> <display-name>se452-taglib</display-name> <small-icon></small-icon> <large-icon></large-icon> <description> A set of example tag libraries for SE452 at DePaul University </description> <tag> <name>date</name> <tag-class>se452.tags.DateTag</tag-class> <body-content>empty</body-content> <description> Prints the current time and date </description> </tag> <tag> <name>debug</name> <tag-class>se452.tags.DebugTag</tag-class> <body-content>JSP</body-content> <description> Includes the contents of the tag if debug request parameter is set to true </description> </tag> <tag> <name>select</name> <tag-class>se452.tags.SelectTag</tag-class> <body-content>empty</body-content> <description> Displays an html select with preselected options </description> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>selected</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>list</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
Deploying Tag Libraries [10/14] |
"doc root"/WEB-INF/classes
<jsp-config> <taglib> <taglib-uri>se452-taglib.tld</taglib-uri> <taglib-location>/WEB-INF/tld/se452-taglib.tld</taglib-location> </taglib> </jsp-config>
Using Tag Libraries [11/14] |
Use taglib directive
<%@ taglib uri="se452-taglib.tld" prefix="se452" %>
Write the tag in the JSP
<se452:tag1>
or
<se452:tag1> Body (any JSP/html/Text) </se452:tag1>
JSTL [12/14] |
The JavaServer Pages Standard Tag Library (JSTL) encapsulates, as simple tags, core functionality common to many JSP applications
JSTL is hosted at Jakarta - along with many other useful tag libraries
JSTL, unlike the other libraries, is standardized through the Java Community Process (JCP).
JSTL comes in two versions:
JSTL is the origin of the Expression Language that is now standard in JSP 2.0, so you can use the EL in any JSTL 1.0 tags in a JSP 1.2 container, but the EL is not available outside of JSTL tags (like in a JSP 2.0 container).
To deploy the JSTL, just follow the same directions as for any tag library.
JSTL tags [13/14] |
JSTL comes in 4 libraries:
c
x
fmt
sql
Core is for management of variables, iteration, output, conditional logic
XML processing is for parsing, selection of XML, XSTL transforms, logic based on XML data
The i18n library supports formatting of things like currency, dates, and numbers, as well as localized output of content based on Locale
The SQL library is not meant to promote good design of database based apps, but provide for easy access to databases and presentation of the data, without a large amount of abstraction.
Homework [14/14] |
Goal: To write a simple tag library from scratch.
SimpleTag
Supply the application as a deployable war file. Test deploying it before submitting it to make sure you have the format right. Place the source files for the servlet in a src directory in the war file. Please link the homework assignment to the index.html file in the document root.
Grading Breakdown:
Item | Points |
---|---|
configuration (web.xml) | 10 |
proper TLD | 20 |
Tag implementation | 30 |
JSP implementation | 20 |
Coding style and JavaDoc | 10 |
Proper Submission | 10 |
Total: | 100 |
Revised: 11/9/2003