Expression Languages


Before we dive into JSTL, we will examine one of its most important features: expression languages . Expression languages give JSP developers a mechanism for embedding expressions to be evaluated in place of constants or scriptlets. Consider the following example, in which a JSP must display the amount attribute contained in the JavaBean called PaymentDetail:

 <jsp:useBean   scope="application" /> The number of items in the catalog is <%=catalog.getItems().getSize() %> <!—Other JSP code—> 

Here, a page author has to use an expression <%= somevalue %> to access the properties of the JavaBean component. If the properties are nested, the expression becomes even more complex:

 <%= catalog.getItems().getItem(0).getName()%> 

An expression language allows a page author to access an object using a simplified syntax such as <x:sometag att="${aName}"> for a simple variable or

 <x:sometag att="${outer.inner.innermost}"> for nested properties. 

JSTL defines the syntax for such expression languages, based on ECMA-Script and XPath. To be more precise, the JSP 2.0 developed under JSR-152 owns the syntax from which the JSTL expressions are derived.

start sidebar

As result of the confusion and cross-browser compatibility issues of Java-Script, Netscape submitted a proposal to the European Computer Manufacturers Association that was adopted in 1997. The home of the ECMA-Script specification is www.ecma.ch.

XPath is used to access different parts of an XML document. It is a W3C specification since 1999 and can be found at www.w3.org/TR/xpath.

end sidebar

The expression language defines a set of implicit objects. When an expression references one of these objects by name, the appropriate object is returned instead of the corresponding attribute. For example, ${pageContext} returns the PageContext object. Most of the implicit objects are modeled as a java.util.Map and hold some name value mapping. Table B.1 lists the implicit objects available.

Table B.1: Implicit objects in JSTL

Implicit Object

Description

pageContext

The PageContext object in JSPs.

pageScope

Maps page-scoped attribute names to their values.

requestScope

Maps request-scoped attribute names to their values. The attributes are available only for the duration of the request.

sessionScope

Maps session-scoped attribute names to their values. Attributes are available until the session is invalidated.

applicationScope

Maps application-scoped attribute names to their values. Attributes are available throughout the application for the life of the container. These map to a ServletContext .getAttribute().

param

Maps parameter names to a single String parameter value using request.getParameter (name).

paramValues

Maps parameter names to a String[] of all values for that parameter using request.getParameter (name).

header

Maps header names to a single String header value using request.getHeader (name).

headerValues

Maps header names to a String[] of all values for the headers.

cookie

Maps cookie names to a single cookie object that is returned using the request.getCookies() method.

initParam

Maps context initialization parameter names to their values using the getInitParameter(name).

For example, to access the URI from the request, the implicit object request can be used, like ${pageContext.request.requestURI}. This will obtain the URI of the request, and the container will map this to HTTPServletRequest.getRequestURI().

To obtain a catalog object from the session, ${sessionScope:catalog} can be used, and the container will call pageContext.getAttribute("catalog", PageContext.SESSION_SCOPE).




Java Web Services Architecture
Java Web Services Architecture (The Morgan Kaufmann Series in Data Management Systems)
ISBN: 1558609008
EAN: 2147483647
Year: 2005
Pages: 210

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net