|
JSTL - Syntax and Expression Language
Part:
1
2
3
4
This chapter explains:
- Basics on JSTL syntax.
- Basics on Expression Language.
- Literal data and named variables.
- Basic operators and operations.
- Accessing collection elements and object properties.
- ExpExample.jsp - Expression examples JSP page.
- pageContext attributes and JSTL top level identifiers.
JSTL Syntax
JSTL syntax is very simple. JSTL allows you to write tags in JSP pages.
Each tag is actually called an action. Every action must be written as an XML element.
The syntax of an JSTL action XML element is something like these:
<p:tag attribute="text_only"/>
<p:tag>
xml_body
</p:tag>
<p:tag attribute="text_only">
xml_body
</p:tag>
<p:tag attribute="${expression}">
xml_body
</p:tag>
<p:tag attribute="text${expression}text${express}..." ...>
xml_body
</p:tag>
As you can see, there are a number variations in the syntax:
- An action can be an empty or non-empty XML element.
- An action can have zero, one, or many attributes.
- Attribute values can be text only, or mixed with expressions.
- An express is always written in the format of ${expression}.
The expressions must be written by folowing rules defined by the expression language.
Examples of JSTL actions:
<c:out value="Hello world!"/>
<c:if test="${1+1==2}">
Always true.
</c:if>
<c:set var="message" value="Hello world!"/>
<c:out value="${message}"/>
Expression Language
Since expression will be used in most of the JSTL actions, let's look at the
express language before individual JSTL actions.
JSTL expression language is inspired by both ECMAScript and XPath expression language.
It is simple, and supporting the following features:
- Literal data and named variables.
- Logical, relational and arithmetic operations.
- A set of implicit objects.
- Nested properties and accessors to collections.
Examples of JSTL expressions:
<c:out value="${1+1==2}"/>
<c:out value="${1+1}"/>
<c:out value="${1/3}"/>
<c:out value="${1.0/3.0}"/>
<c:out value="${message}"/>
<c:out value="${pageContext.request.class.name}"/>
<c:out value="${pageContext.request.method}"/>
<c:out value="${pageContext.request.requestedSessionId}"/>
<c:out value="${pageContext.request.cookies[0].name}"/>
<c:out value="${quantity*price < 100.0 && country=='USA'}"/>
Literal Data and Named Variables
As in all computer language, expression starts with literal data and variables.
JSTL expression language supports 5 types of literal data:
- Boolean - true and false. Same as Java boolean.
- Integer - like 9999, -3, and 0. Same as Java long.
- Floating point number - like, 1.0, 3.14159, and -1.0e-3. Same as Java double.
- String - like, 'USA', "USA", or 'Herong\'s notes. Close to Java String.
- Null - null. Same as Java null.
(Continued on next part...)
Part:
1
2
3
4
|