Você está na página 1de 4

JSTL Core Library

JSTL core library can be introduced to a JSP page with:


<jsp:root
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jstl/cor
e" version="1.2">
It provides the following basic scripting functions:
<c:out value="..."/>
<c:set var="..." value="..."/>
<c:if test="...">body</c:if>
<c:choose>body</c:choose>
<c:forEach items="...">body</c:forEach>
<c:forTokens items="..."
delims="...">body</c:forTokens>
c:out Action
<c:out
value="text_mixed_with_expressions"/>
The "value" attribute will be evaluated and the resulting
value will be converted into a string which will be inserted
into the HTTP response. The c:out action serves similar
purposes as the JSP expression element.
c:set Action
<c:set var="name" value="expression"/>
The "var" attribute specifies a variable name, which will be
declared and assigned with the value resulted from the
"value" attribute. The c:set action serves similar purposes as
the Java assignment statement.
c:if Action
<c:if test="expression"/>
body
</c:if>
If the "test" attribute is evaluated to true, body will be
processed. The c:if action serves similar purposes as the
Java if statement.
c:choose Action
<c:choose>
<c:when test="expression">
body
</c:when>
<c:when test="expression">
body
</c:when>
...
<c:otherwise>
body
</c:otherwise>
</c:choose>
The body of the first c:when action that has the "test"
attribute evaluated to true will be processed. If all "test"
attributes are evaluated to false, the body of the c:otherwise
action will be processed. The c:choose action serves similar
purposes as the Java if-else statement.
c:forEach Action
<c:forEach var="name" items="expression"
begin="expression" end="expression"
step="expression">
body
</c:forEach>
If the "items" attribute is specified, it will be used to
identify an array or collection object, the elements in the
array or collection will be iterated. At each iteration, the
current element will assign to a named variable specified in
the "var" attribute, and body will be processed. If the
"begin", "end" or "step" attribute is specified, it will be used
to restrict the beginning element, the ending element, or the
step size of the iteration.
If the "items" attribute is not specified, an index integer will
be used to iterate from the "begin" value to the "end" value
stepping with the "step" value.
The c:forEach action serves similar purposes as the Java for
statement. But there is no break mechanism.
c:forTokens Action
<c:forTokens var="name"
items="expression" delims="expression"
begin="expression" end="expression"
step="expression">
body
</c:forTokens>
The string specified by the "items" attribute will be
tokenized with the delimiter specified by the "delims"
attribute. Then resulting tokens will be iterated. At each
iteration, the current token will assign to a named variable
specified in the "var" attribute, and body will be processed.
If the "begin", "end" or "step" attribute is specified, it will
be used to restrict the beginning element, the ending
element, or the step size of the iteration.

The empty Operator


Testing for the existence of request parameters can be tricky
because they evaluate to null if they don't exist but they
evaluate to an empty string ("") if their value was not
specified. Most of the time, when you check for the
existence of request parameters, you don't have to
distinguish the former from the latter; you just want to
know whether a value was specified. For that special task,
you can use the empty operator, which tests whether an
identifier is null or doesn't exist, as illustrated by the