Page 3 of 6
${alist[4]}
${aList[someVariable]}
Both JavaBean properties and java.util.Map elements (which represent a set of name/value pairs) can be accessed using one of the following ways. In the first two expressions
below, we can access a property named aProperty in a JavaBean or a Map entry with the key aProperty. In the third expression (note I've left out the quotes), we access an element in anObject with a name held in the variable aVariableContainingPropertyName:
${anObject.aProperty}
${anObject["aPropertyName"]}
${anObject[aVariableContainingPropertyName]}
There are a number of implicit varibles defined in the EL:
pageContext: the pageContext object for that Webpage
pageScope, requestScope, sessionScope, and applicationScope: these are Map collections that map variable names in each of these scopes to values
param and paramValues: parameters passed with the page request; same as in JSP
header and headerValues: headers passed with the page request; same as in JSP
cookie: Map that maps cookie names to a particular cookie object
The EL defines a full set of operators that corresponds closely to those you're familiar with in Java. Arithmetic operators
include +, -, *, / (or div), and % (or mod). Relational operators include ==, !=, <, >, <=, >=, which correspond to eq, ne, lt, gt, le, and ge, respectively. I won't elaborate on these operators because they are all self-explanatory.
Now that I've covered some basics and looked at EL syntax, I can discuss the four JSTL tag libraries specifically. I discuss the core library most since it's the one you'll certainly use; but I'll also cover the rest in enough detail to get you started.
First, though, I should talk more about the JSTL tag libraries' two flavors. I mentioned above that each JSTL tag library comes in two versions: one that supports expressions in the EL and one that supports standard JSP expressions. When you import any tag library into a JSP page, you define a prefix that designates a namespace corresponding to the tags in that library.
The four standard tag libraries, with their JSTL spec-defined prefix conventions, are listed below. Note that you could define your own prefixes, but there is absolutely no good reason for this.
Four standard tag libraries
|
To use the EL core tag library in your page (you're really just giving your page visibility into the namespace defined in the library), include the following example directive at the top of your page:
<%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>
To use the tags in that core library, prefix each tag in your page with the prefix you've designated in your include statement:
<c:out value="${anExpression}"/>
Let's examine the core tag library in more detail. We look at the most commonly used functionality first.
The core library's most basic tag is the c:out tag, which displays an EL expression's value in a page. An example expression that uses c:out might look like this:
We have <c:out value="${applicationScope.product.inventoryCount}" escapeXml="true" default="0" /> of those items in stock.
In the above, the value attribute is the expression we send to the page output. I've also shown the optional escapeXml attribute, which specifies whether XML characters (<, >, &, and .) should convert to corresponding character entity codes (so they show up as those characters in an HTML page), and the default
attribute, which is used if the EL can't evaluate the value or the value evaluates to null.
Note that when EL support is fully implemented in JSP 2.0, you won't need to use the c:out action; you can just embed JSP expressions directly in the page.
Another commonly used core action is c:set, which sets a variable in a page. You can use c:set in two ways. The first way sets the variable defined in the var attribute to the value defined in the value attribute, as shown below:
<c:set var="customerID" value="$param:customerNumber" scope="session" />
The optional scope attribute above specifies that we want to set the variable customerID in the session scope; if scope is not specified, it defaults to page scope.
Another powerful usage of c:set assigns the contents of the c:set tag's body to a specified variable:
<c:set var="cellContents">
<td>
<c:out value="${myCell}"/>
</td>
</c:set>
In the above example, a c:set action defines a variable named cellContents (in page scope) that holds the contents defined in the tag's body. In this case, the body defines an HTML table cell element.
The c:out action in the body is evaluated, and that evaluation's results are included in the string literal in the body.
As you might expect, JSTL has made exception handling a bit easier. In typical JSP pages, you have two approaches for handling
exceptions: try/catch blocks in scriptlet code embedded directly in the page or with a JSP errorPage directive that provides a nice catch-all way to handle any possible exception on a page. JSTL offers a good alternative with
the c:catch action, which provides an effective way to handle exceptions with a bit more granularity without embedding Java code in your
pages. A c:catch action might look like this:
<c:catch>
<!--. . . some set of nested JSTL tags below which would be hit on an exception-->
</c:catch>
The c:catch action has an optional attribute, a variable that references a thrown exception.
You're less likely to use the c:remove tag. This tag has attributes for a variable name and a scope, and removes the specified variable from the specified scope.
Let's move on to JSTL's flow control and conditional tags. If you've used conditional and flow control statements in any language, conceptually there's not much new here.
The c:if action handles simple conditional tests. The Boolean expression's value in the test attribute is evaluated; if true, the
body's contents are evaluated. In the action below, we also show the optional var attribute that stores the test results for later use in the page (or elsewhere, if the other optional scope attribute is specified):
For those reading this article, *don't* use the method specifiedBy Anonymous on October 18, 2009, 9:59 amFor those reading this article, *don't* use the method specified above to get JSTL to work with your JSP pages. A better and standard approach is to place the "jstl.jar"...
Reply | Read entire comment
wowBy Anonymous on October 13, 2009, 2:02 amwow
Reply | Read entire comment
testBy Anonymous on September 2, 2009, 1:56 pmtest
Reply | Read entire comment
By Anonymous on October 29, 2008, 7:28 am
Reply | Read entire comment
View all comments