Java Examples In A Nutshell (3rd Edition) [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java Examples In A Nutshell (3rd Edition) [Electronic resources] - نسخه متنی

O'Reilly Media, Inc

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








20.6 Hello JSP2


The JSP 2.0 specification is a major step forward from the 1.x
series. It includes support for a simple Java-like expression
language (known as "EL"), which
replaces awkward <%=...%> tags, and supports
the Java Standard Tag Libraries
(JSTL), which obviate the need for most
<%...%> scriptlets. Example 20-4,
rewritten using these features. Note that
<c:choose> and
<c:when> tags serve as an
if/else statement and replace the awkwardly
intermingled HTML tags and Java scriptlets of Example 20-4. Also, notice that EL expressions are enclosed
in ${...}, which is quite a bit friendlier than
<%...%>. In this example, the
@page directive is joined by an
@taglib directive specifying that the page uses
the standard "core" tag library and
declares its XML namespace to be
"c".

Example 20-5. hello2.jsp

<%@page contentType='text/html'%>
<%-- The taglib directive specifies that we're using
the JSTL 1.1 core taglib.
-- If you're using 1.0, change to "http://java.sun.com/jstl/core_rt" --%>
<%@taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<c:choose>
<c:when test='${param.name == null}'>
<form>
<i>Please enter your name: </i>
<input name="name"/>
<input type="Submit"/>
</form>
</c:when>
<c:otherwise>
Hello ${param.name}!
</c:otherwise>
</c:choose>


20.6.1 Expression Language Syntax


A full tutorial on the Expression Language used in JSP 2
pages is beyond the scope of this chapter; you can find complete
details in the JSP 2.0 specification. For now, however,
we'll simply note that the language supports
expressions and operators much like Java and JavaScript do. The
. and [ ] operators can be used
to dereference objects. Objects that follow JavaBeans naming
conventions behave as if they have properties (rather than accessor
methods). Map objects with String keys can also be
dereferenced with the . and [ ]
operators.

JSP 2.0 defines the predefined objects listed in Table 20-2; these objects can be used in EL expressions.

Table 20-2. JSP 2.0 predefined objects

Object name


Represents


applicationScope


Maps application-scoped attribute names to values. See
ServletContext.setAttribute( ) and the JSTL
<c:set> tag.


cookie


Maps cookie names to Cookie objects.


header


Maps header names to header values as single strings. See
ServletRequest.getHeader( ).


headerValues


Maps header names to header values as a String[ ].
See ServletRequest.getHeaders( ).


initParam


Maps initialization parameter names to their
String values. See
ServletContext.getInitParameter( ).


pageContext


The
javax.servlet.jsp.PageContext object for the page.


pageScope


Maps page-scoped attribute names to values. See
PageContext.setAttribute( ) and the JSTL
<c:set> tag.


requestScope


Maps request-scoped attribute names to values. See
ServletRequest.setAttribute( ) and the JSTL
<c:set> tag.


sessionScope


Maps session-scoped attribute names to values. See
HttpSession.setAttribute( ) and the JSTL
<c:set> tag.


param


Maps request parameters to single String values. See
ServletRequest.getParameter( ).


paramValues


Maps request parameters to String[ ] arrays. See
ServletRequest.getParameterValues( ).

JSP 2.0 also includes support for predefined functions in
EL
expressions. JSTL 1.1 defines commonly used functions in a module
that you can use just a like a tag library. To use these functions,
include the following @taglib directive in your
page:

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

This declaration makes the following functions available to EL
expressions. For the most part, these functions behave just like a
well-known function of the same name in the core Java API:

boolean fn:contains(String, String)
boolean fn:containsIgnoreCase(String, String)
boolean fn:endsWith(String, String)
boolean fn:startsWith(String, String)
int fn:indexOf(String, String)
int fn:length(Object)
String fn:escapeXml(String)
String fn:join(String[ ], String)
String fn:replace(String, String, String)
String fn:substring(String, int, int)
String fn:substringAfter(String, String)
String fn:substringBefore(String, String)
String fn:toLowerCase(String)
String fn:toUpperCase(String)
String fn:trim(String)
String[ ] fn:split(String, String)


20.6.2 Displaying JSP Pages that Use Tag Libraries


If you use the je3.war file, the URL to display
this example is almost identical to the last one:

http://localhost:8080/je3/hello2.jsp

Note, however, that in order to make it work, the servlet container
must be able to find the tag library implementation classes, as
described at the beginning of this chapter. If you are using Tomcat,
make sure that you've put the
jstl.jar and standard.jar
files (or more current replacements for these files) in the
common/lib/ directory. Note that the
standard.jar file contains the necessary tag
library descriptor (TLD) files and that the file
META-INF/c.tld defines the URI used in the
@taglib directive in Example 20-5.

Example 20-5 uses Version 1.1 of the JSTL. If you
cannot find 1.1 implementation classes, you should be able to make
the 1.1 classes work in a 1.0 implementation. To do this, though, you
must change your @tablib directive to use the
slightly different URL that identifies Version 1.0:

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

Finally, note that this example uses only the core module of the
JSTL. In addition to the functions module mentioned previously, the
JSTL also includes modules of tags for database access
("sql"), XML manipulation
("x"), and internationalized text
formatting ("fmt").

/ 285