(last updated: Dec 15, 2006)
Select font size:
8pt 9pt 10pt 11pt 12pt 14pt 16pt
Download the
JSTLexamples.war
archive. This archive contains the following files:
index.jsp Script with a variety of examples
bean/MyBean.java User-defined bean example
mytaglib/MyFunctions.java JSTL tag library classes
Functions.tld JSTL tag library definition
Install and run in Eclipse
Prior to starting eclipse, you need to make available two new library JAR files which are included in the apache-tomcat distribution but not available for all applications. Locate the directory:
apache-tomcat-6.0.14/webapps/jsp-examples/WEB-INF/lib/
and within it, the files:
jstl.jar standard.jar
Simply copy these two files into:
apache-tomcat-6.0.14/common/lib/
Then start up eclipse. Follow the steps in JSP Forms import the WAR file JSTLexamples.war and run the project on Tomcat.
The JSP Standard Tag Library (JSTL)
The JSTL library provides a means, through variables, expressions and tags, to avoid the interleaving of Java and tag-style code. Using this language, we can, for the most part, eliminate explicit Java code in JSP pages in favor of augmenting the XML-like tag structure. See
http://jakarta.apache.org/taglibs/
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/
You should refer to this online documentation:
jstl tld docs
and/or download it yourself:
jstl-1_1-mr2-spec-tlddoc.zip
The standard tags are those described in the table below. We have omitted the sql tags since we don’t plan to use SQL done directly in JSP. Using one of tag groups is effected by the addition of one of the following respective tag statements below.
c: <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
fn: <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
fmt: <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
xml: <%@taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
The uri is simply an identifier of the tag set. The prefix is determinable by the user, but the prefixes seen here are standard.
TLD tags and functions c: core fmt: formatting fn: functions x: xml
c:catch
c:choose
c:forEach
c:forTokens
c:if
c:import
c:otherwise
c:out
c:param
c:redirect
c:remove
c:set
c:url
c:when
fmt:bundle
fmt:formatDate
fmt:formatNumber
fmt:message
fmt:param
fmt:parseDate
fmt:parseNumber
fmt:requestEncoding
fmt:setBundle
fmt:setLocale
fmt:setTimeZone
fmt:timeZone
fn:contains()
fn:containsIgnoreCase()
fn:endsWith()
fn:escapeXml()
fn:indexOf()
fn:join()
fn:length()
fn:replace()
fn:split()
fn:startsWith()
fn:substring()
fn:substringAfter()
fn:substringBefore()
fn:toLowerCase()
fn:toUpperCase()
fn:trim()
x:choose
x:forEach
x:if
x:otherwise
x:out
x:param
x:parse
x:set
x:transform
x:when
These and other tag systems are used in JSP in one of two forms:
or, as functions to create expressions:
tag-prefix:function(arg1,arg2,...)
As in XML, tags can be complete, using a single tag ending with “/>“, or in pairs, containing a content terminated by a matching ending tag . Some example of the common usages of the “c” tags are these:
Expression Language (EL)
The syntax for what replaces the “…” above is specified by a Java-like language, unimaginately called Expression Language (EL). EL is part of JSP; its expressions are all surrounded by the syntax ${ }. but typically can only be used most effectively in conjunction with JSTL tags. EL is closely related to Java but has some extensions and conceptual simplifications. In particular, EL is more like other web script languages being loosely typed along with other semantic simplifications. EL expressions permit variables and most of the usual arithmetic and boolean operators. Here are some points:
* The == operator for strings functions like the Java .equals operator.
* an EL expression with an undefined value, which (normally represented by null in Java) is also represented by null in EL, but is equivalent to the empty string.
* EL has a unary operator empty: empty(x) acts like the expression x==null but also means “x equals the empty string”.
* the operators or, and are synonyms for ||, &&, respectively.
Query parameters
The value of the parameter “xx” is expressed by the EL expression param.xx. When used directly in HTML,
${param.xx}
is equivalent to the JSP expression:
<%= (request.getParameter("xx") != null) ? (request.getParameter("xx") : "" %>
Session variables are EL variables
A session variable x automatically becomes available as an EL variable, e.g. if we have:
<%
session.setAttribute( "x", "hello" );
// or
pageContext.setAttribute( "x", "hello" );
%>
...
x = ${x}
${x}
When using a map, the iteration generates Map.Entry pairs. Using the getKey and getValue functions the structure of the JSTL code to iteratively print the key/value pairs looks like this:
${x.key}: ${x.value}
Choice tags c:if, c:choose/c:when
The c:if tag generates a choice situation with a syntax like:
The EL_BOOLEAN_EXPRESSION uses the usual boolean operators with simplifications and additions as mentioned above. If-else structures are based on the c:choose/c:when/c:otherwise tags:
...
User-generated tag functions
If one wants to avoid explicit JSP sections of code then the Java classes and functions must be able to be accessed through the tag system. Beans provide a limited way of doing so through combinations of “get/set” member functions, but this is not intended for utility functions to manipulate objects. The fn tags provide some of these utility functions, but one can imagine that soon there would be need for your own specific utility functions. Towards this end, JSP provides a means to add your own tag libraries using a combination of Java classes and Tag Library Definition (TLD) files. Both XML-like tags and EL function tags can be added, but it’s much easier to create user-defined EL function tags. The utility functions used must be static functions. For example, suppose we want to use the function defined in this class in an EL expression.
mytaglib/MyFunctions.java
package mytaglib;
import java.util.*;
public class MyFunctions {
public static boolean contains(Set
return set.contains(target);
}
}
The glue which connects this functions to JSP is the XML file MyTags.tld held stored in the /WEB-INF/ project directory.
MyTags.tld
boolean contains(java.util.Set,java.lang.String)
This file defines a uri by which it is accessed and a set of function prototypes in XML format. Each function has a tag name, the class it belongs to and the actual function prototype within this class. The necessary specification within the JSP file is something like this:
<%@taglib prefix="mtg" uri="/WEB-INF/MyTags"%>
The prefix chosen specifies how the function will be referenced within the EL syntax — it can be anything you want, so long as it doesn’t conflict with another tag set. In this case the function will be prefaced by mtg: as in this example:
Example script
index.jsp
<%@ page language=”java” contentType=”text/html;
charset=US-ASCII” pageEncoding=”US-ASCII”%>
<%@page import=”java.util.*”%>
<%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>
<%@taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions”%>
<%@taglib prefix=”mtg” uri=”/WEB-INF/MyTags”%>
<%
String x = “testing”;
String[] a = { “xx”, “yy”, “zz” };
Map
m.put(”aa”, “1″);
m.put(”bb”, “2″);
m.put(”cc”, “3″);
List
l.add(”nn”);
l.add(”mm”);
l.add(”oo”);
Set
s.add(”aa”);
s.add(”bb”);
s.add(”cc”);
session.setAttribute(”x”, x);
session.setAttribute(”a”, a);
session.setAttribute(”m”, m);
session.setAttribute(”l”, l);
session.setAttribute(”s”, s);
%>
access values of query parameter p
first value: ${param.p}
all values: ${fn:join(paramValues.p,”,”)}
print objects
x: ${x}
m: ${m}
s: ${s}
l: ${l}
print array using join
a: ${fn:join(a,”,”)}
access individuals of array or map via [ ]
a[1]: ${a[1]}
m["bb"]: ${m["bb"]}
access individuals in c:forEach loop
a:
s:
l:
m:
using c:set and escapeXml
k: ${k}
escaped(k): ${fn:escapeXml(k)}
c:out escaped:
using a bean’s get and set properties indirectly
mb.count: ${mb.count}:
became bigger than 6, re-initialize mb
User defined tag functions
s: ${s}
s.contains(${t}): ${mtg:contains(s,t)}
s.contains(${t}): ${mtg:contains(s,t)}
No comments:
Post a Comment