Transcript Chapter 7
Chapter 7
Using Custom Tag Libraries and
the JSP Standard Tag Library
What Is a Custom Tag Library?
Custom Actions
Usage
access to all information about the request
add content to the response body as well as set
response headers
use any Java API to access external resources
such as databases, email servers, etc.
<prefix:tag atttribute=“value” />
Custom Tag Implementations
Java Classes
package com.ora.jsp.tags.motd;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.ora.jsp.beans.motd.*;
public class MixedMessageTag extends SimpleTagSupport {
private MixedMessageBean mmb =
new MixedMessageBean( );
// Attributes
private String category;
public void setCategory(String category) {
this.category = category;
}
public void doTag( ) throws IOException {
mmb.setCategory(category);
JspWriter out = getJspContext().getOut(
out.println(mmb.getMessage( ));
}
}
);
Custom Tag Implementations
Tag files (An example)
<%@ tag body-content="empty" %>
<%@ attribute name="category" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="mmb" class="com.ora.jsp.beans.motd.MixedMessageBean" />
<c:set target="${mmb}" property="category" value="${category}" />
${mmb.message}
Custom Tag Implementations
Custom Tag Library
Tag Library
Descriptor (TLD)
JAR
<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>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>mytags</short-name>
<uri>mytaglib</uri>
<tag-file>
<name>copyright</name>
<path>/WEB-INF/tags/mytags/copyright.tag</path>
</tag-file>
<tag-file>
<name>forEvenAndOdd</name>
<path>/WEB-INF/tags/mytags/forEvenAndOdd.tag</path>
</tag-file>
<tag-file>
<name>htmlFormat</name>
<path>/WEB-INF/tags/mytags/htmlFormat.tag</path>
</tag-file>
</taglib>
Installing a Custom Tag Library
place the JAR file for the library in the
WEB-INF/lib directory for the web application
Declaring a Custom Tag Library
Prefix
use any prefix you like
except jsp, jspx, java, javax,
servlet, sun, or sunw
Uri
When the web application is
started, the container scans
through the WEB-INF
directory structure for files
with .tld extensions and all
JAR files containing files
with .tld extensions in their
META-INF directory
<%@ page contentType="text/html" %>
<%@ taglib prefix="ora" uri="orataglib" %>
<html>
<head>
<title>Messages of the Day</title>
</head>
<body bgcolor="white">
<h1>Messages of the Day</h1>
<h2>Deep Thoughts - by Jack Handey</h2>
<i>
<ora:motd category="thoughts" />
</i>
<h2>Quotes From the Famous and the Unknown</h2>
<i>
<ora:motd category="quotes" />
</i>
</body>
</html>
Declaring a Custom Tag Library
Relation between the taglib directive, the TLD, and the
implementation (tag handler) for the custom actions
Identifying a Custom Tag Library in a
JSP 1.1 Container
WEB-INF/web.xml
<web-app>
...
<taglib>
<taglib-uri>
orataglib
</taglib-uri>
<taglib-location>
/WEB-INF/lib/orataglib_3_0.jar
</taglib-location>
</taglib>
...
</web-app>
<%@ taglib uri="/WEB-INF/lib/orataglib_3_0.jar" prefix="ora" %>
Using Actions from a Tag Library
<%@ page contentType="text/html" %>
<%@ taglib prefix="ora" uri="orataglib" %>
<html>
<head>
<title>Messages of the Day</title>
</head>
<body bgcolor="white">
<h1>Messages of the Day</h1>
<h2>Deep Thoughts - by Jack Handey</h2>
<i>
<ora:motd category="thoughts" />
</i>
<h2>Quotes From the Famous and the Unknown</h2>
<i>
<ora:motd category="quotes" />
</i>
</body>
</html>
Setting Action Attribute Values
Action attributes can be of any Java type
Automatic type conversion
The JSP Standard Tag Library
URI for the JSTL 1.1 libraries
Library
URI
Prefix
Core
http://java.sun.com/jsp/jstl/core
c
XML processing
http://java.sun.com/jsp/jstl/xml
x
I18N formatting
http://java.sun.com/jsp/jstl/fmt
fmt
Database access
http://java.sun.com/jsp/jstl/sql
sql
Functions
http://java.sun.com/jsp/jstl/functions
fn
JSTL functions
Function call syntax
fn:contains(string, substring)
Description
Returns true if the string contains the
substring
fn:containsIgnoreCase(string,
substring)
Returns true if the string contains the
substring, ignoring case
fn:endsWith(string, suffix)
Returns true if the string ends with the suffix
Returns the string with all characters that have
fn:escapeXml(string)
fn:indexOf(string, substring)
special meaning in XML converted to their
equivalent XML character entity code
Returns the index for the first occurrence of
the substring in the string
JSTL functions
fn:join(array, separator)
Returns a string composed from the array elements,
separated by the separator
fn:length(item)
Returns the number of elements in the item if it's a
collection or array, or the number of characters in the
item if it's a string
fn:replace(string, before, Returns a string where all occurrences of the before
after)
string have been replaced with the after string
fn:split(string,
Returns an array where the elements are the parts of the
separator)
string that are separated by the separator
fn:startsWith(string,
prefix)
Returns true if the string starts with the prefix
fn:substring(string,
begin, end)
Returns a part of the string, starting from the begin
index up to and including the end index
JSTL functions
fn:substringAfter(string,
substring)
Returns the part of the string that follows the
substring
fn:substringBefore(string,
Returns the part of the string that precedes the
substring)
substring
fn:toLowerCase(string)
fn:toUpperCase(string)
fn:trim(string)
Returns a string with all characters from the
input converted to lowercase
Returns a string with all characters from the
input string converted to uppercase
Returns a string with all leading and trailing
whitespace characters in the input string
removed
JSTL Reference Implementation JAR files
File
Description
dom.jar
W3C DOM classes, used by the JSTL XML library
implementation. Part of JAXP 1.2. It can be removed if you use
JDK 1.4.2 or later.
jaxp-api.jar
Java API for XML Processing (JAXP) 1.2 specification classes,
used by the JSTL XML library implementation. It can be
removed if you use JDK 1.4.2 or later.
JDBC 2.0 Optional Package specification interfaces, used by the
jdbc2_0-stdext.jar
jstl.jar
JSTL SQL library implementation. Also bundled with Java 2
SDK 1.4 as well as with Tomcat 4, independent of SDK version;
it can be removed when using one of these environments.
JSTL specification classes and interfaces.
JSTL Reference Implementation JAR files
XML.org SAX classes, used by the JSTL XML library
sax.jar
standard.jar
implementation. Part of JAXP 1.2. It can be removed if you use JDK
1.4.2 or later.
The reference implementation for all JSTL classes and interfaces,
developed by the Apache Taglibs project. This is the main JAR file for
the JSTL RI.
Apache Xalan XSLT processor, used by the XML JSTL library
xalan.jar
implementation. It can be removed if you use Sun's JDK 1.4.2 or later,
but it may be needed if you use another vendor's JDK 1.4.2.
xercesImpl.jar
Apache Xerces XML parser, used by the XML JSTL library
implementation. It can be removed if you use JDK 1.4.2 or later.
Using Beans or Custom Actions
a bean is a great carrier of information
a custom action is great for processing
information
Custom actions can use beans as input
and output