|
JSP - Part 3
The JSP Standard Tag Library (JSTL) provides a set of tags to perform common generic functions, such as 'if then else'. TJI provides the main tags of the JSTL 'core' in source code format (thereby providing a resource for studying how tag handlers can be written). This document also looks at how attributes can be stored with various scopes - as with Java Beans in JSP. Five new example projects demonstrate the new functionality very well and can be found on the 'Resources' page of our web site. These include a JSP version of 'Duke's Book Store' to parallel the servlet based version.
The Expression Language Consider the following snippet of JSP :
It's a bit hard to read. Now look at this alternative :
Much cleaner looking! Welcome to the 'Expression Language' - or 'EL' for short. The EL version (above) is easier to read because it avoids having angle brackets inside angle brackets. Furthermore it employs a short hand for referring to:
Retrieving attributes You may remember attributes from working with Servlets. You can use EL to retrieve a page scope attribute (the default scope) with, for example:
This is equivalent to :
The JSP enabled server will look for an attribute called 'count' in the page, request, session and application scopes (in this order) and return its value. If 'count' is not found, null is returned.
Retrieving Bean Properties If count is a bean property accessed by method getCount(), it can be accessed by the following Expression Language:
See also <c:out value="xxx"> below.
A comparison of Scriptlets, Expressions and the use of the Expression Language Consider a Web application that maintains information about usage statistics in a bean of class ServerStats. Class ServerStats has a method getUserCount(), which returns the number of users currently logged on. The application maintains a single instance of ServerStats in a ServletContext (i.e. application scope) attribute called 'stats'. Here's a comparison of how you could create usage statistics output using a scriptlet, a JSP expression, and by using the Expression Language.
There are currently ${stats.userCount} users logged in.
Of course, an alternative is to use a custom tag. Custom tags produce a cleaner JSP page and enable code reuse. However, this approach is probably 'over the top' in this case. Remembering that the implementation code is in the handler class, the JSP code for the example message would look like this: There are currently <myTags:userCount/> users logged in.
Predefined Implicit Objects
can also be written as : or simply : which as a standard expression would be written as :
There is also 'param'. For example, which is equivalent to:
This is used when request parameters may have multiple values with the same key. For example, a form may send the following request:
The following snippet shows how paramValues can be used to get at the multiple values one by one - by using the standard tag c:forEach (see below)
So, for example, will return the attribute named "count" defined in scope 'session' - or null if it does not exist. See the 'Attribute Scope' section of this document for information on scope.
Notes on the support for Expression Language in TJI The Expression Language supported by TJI is not a complete support of its functionality. Nevertheless, quite complex expressions are possible. For example: The point is, don't try to put too much business logic into an expression - try to put it into a bean or custom tag instead. For example:
or <myTags:foodValid>...</myTags:foodValid> In the above case, the matter is borderline. Certainly any complex logic should be 'off-page'. Note that TJI supports the standard Java symbols for expressions rather than the EL equivalents - '==' rather than 'eq'', '!' rather than 'not', etc.
The JSP Standard Tag Library TJI supports the core part of the JSP Standard Tag Library (JSTL) by providing as a project the core tag handlers, in source code format. This is available on our web site within the 'Resources' page. Simply download the zip file, import it into TJI and build it. You can then use the standard tag handlers in your JSP projects. JSP examples 1, 4 and 5 on our web site provide simple examples that use the standard tag library functionality. There is also a JSP version of 'Duke's Book Store' available. The core tag handlers have a prefix of 'c'. Here are some examples of how they can be used.
The test attribute is mandatory and should return a boolean value. If true, the body is 'run'. A jsp snippet Hello
c:when is almost identical to c:if c:choose groups the c:when tags and runs c:otherise if no c:when has run. Take a look at the source code to see how it works. Basically, c:choose maintains a flag which is set if any c:when child runs; if none has then when it is the turn of c:otherwise, c:otherwise runs. The whole structure is similar to switch in Java. A jsp snippet This page has been accessed<c:choose>
items can be any Collection type. var is optional - it is used as a reference to the current object in the Collection and can be referred to with Expression Language, as in the example below. The jsp file <html><body><jsp:useBean id='bean' class='SimpleBean3'/> The Java Bean import java.util.*;
<p> is equivalent to : <p> One reason for using c:out is to take advantage of the default attribute; this will be used if the value is null. For example: <p>
This tag can be used to set :
scope is optional; if omitted, the default scope is used - 'page' scope. If no target is specified, an attribute is set, and created first if it does not already exist. If target is specified, this should be the name (identifier, not class name) of a bean. value can be any Java object type (not a primitive). So the two forms of use for c:set are :
Attribute Scope Attributes provide a way to store and retrieve information. There are four levels of storage, each with its own scope. The level of scope determines for what duration the attribute is stored. The four scopes are, from shortest to longest duration :
These PageContext constants can be used in the following PageContext methods :
* Page scope is the briefest scope - the attribute exists only as long as the declaring page is active. * Request scope allows the attribute to exist as long as the request, so it will exist in a page forwarded to, or after return from an include. * Session scope ties the attribute to a particular user for a certain amount of time - typically 2 hours - as defined by cookie life or server setting. * Application scope is 'forever' - well, until the server is rebooted.
the default scope is used - page scope.
PageContext page=(PageContext)getJspContext();
<c:set var="x" value="y" scope="session"/> Scope is optional; if omitted, the default scope is used - 'page' scope.
|