JSTL Tag Libraries
Functional Area URI Prefix
Core http://java.sun.com/jsp/jstl/core c
XML processing http://java.sun.com/jsp/jstl/xml x
I18N capable formatting http://java.sun.com/jsp/jstl/fmt fmt
relational db access(SQL) http://java.sun.com/jsp/jstl/sql sql
Functions http://java.sun.com/jsp/jstl/functions fn
Container Requirement
JSTL 1.1 requires a JSP 2.0 web container. Please note that the expression language is part of the JSP specification starting with JSP 2.0.
As defined by the JSP specification, the body content type can be one of empty,JSP, or tagdependent.
<c:set var=”varName” [scope=”{page|request|session|application}”]
value=”value”/>
<c:forEach var=”customer” items=”${customers}”>
Current customer is <c:out value=”${customer}”/>
</c:forEach>
The action must create the variable according to the semantics of PageContext.setAttribute(varName, PAGE_SCOPE), and it must remove it at the end of the action according to the semantics of
PageContext.removeAttribute(varName, PAGE_SCOPE)
White Spaces
Following the JSP specification (as well as the XML and XSLT specifications),whitespace characters are #x20, #x9, #xD, or #xA.
JSTL. Recommended tag prefixes are kept lowercase.as well as attributes such as docSystemId and varDom
Errors and Exceptions
For instance, if <c:forEach> were to throw an exception when given a null value for the attribute items, it would be impossible to easily loop over a possibly missing string array that represents check-box selection in an HTML form (retrieved with an EL expression like ${paramValues.selections}). A better choice is to do nothing in this case.
it is possible in JSTL to define the resource
bundle used by I18N actions via the deployment descriptor (web.xml) as follows:
<web-app>
...
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</
param-name>
<param-value>com.acme.MyResources</param-value>
</context-param>
...
</web-app>
Default Values
It is often desirable to display a default value if the output of an action yields a null value. This can be done in a generic way in JSTL by exporting the output of an action via attribute var, and then displaying the value of that scoped variable with action <c:out>.
For example:
<fmt:formatDate var=”formattedDate” value=”${reservationDate}”/>
Date: <c:out value=”${formattedDate}” default=”not specified”/>
<c:if test="${book.price <= user.preferences.spendingLimit}">
The book ${book.title} fits your budget!
</c:if>
For example, the following would display “Price of productName is productPrice” for a list of products.
<c:forEach var=”product" items=”${products}”>
<c:out value=”Price of ${product.name} is ${product.price}”/>
</c:forEach>
JSP scoped variable returned by a call to PageContext.findAttribute(identifier). This variable can therefore reside in any of the four JSP scopes: page, request, session, or application. A null value is
returned if the variable does not exist in any of the scopes
The code below displays all request parameters along with all their associated values.
<c:forEach var="aParam" items="${paramValues}">
param: ${aParam.key}
values:
<c:forEach var="aValue" items="${aParam.value}">
${aValue}
</c:forEach>
<br>
</c:forEach>
Request headers are also accessible in a similar fashion via implicit objects header and headerValues. initParam gives access to context initialization parameters, while cookie exposes cookies received in the request.
The “[]” operator allows for more generalized access, as shown below:
<%-- “productDir” is a Map object containing the description of
products, “preferences” is a Map object containing the
preferences of a user --%>
product:
${productDir[product.custId]}
shipping preference:
${user.preferences[“shipping”]}
Operators
A very useful “empty” operator is also provided.
The six standard relational operators are supported:
== (or eq), != (or ne), < (or lt), > (or gt), <= (or le), >= (or ge).
The second versions of the last 4 operators are made available to avoid having to use entity references in XML syntax.
Arithmetic operators consist of addition (+), substraction (-), multiplication (*),division (/ or div), and remainder/modulo (% or mod).
Logical operators consist of && (or and), || (or or), and ! (or not).
The empty operator is a prefix operator that can used to determine if a value is null or empty. For example:
<c:if test=”${empty param.name}”>
Please specify your name.
</c:if>
Automatic Type Conversion
For example, if request attributes beginValue and endValue are Integer objects,they will automatically be coerced to ints when used with the <c:forEach> action.
<c:forEach begin=”${requestScope.beginValue}” end=”${requestScope.endValue}”>
...
</c:forEach>
In the example below, the parameter String value param.start is coerced to a number and is then added to 10 to yield an int value for attribute begin.
<c:forEach items=”${products}” begin=”${param.start + 10}”>
...
</c:forEach>
In the following example, the expression ”${user.address.city}” evaluates to null rather than throwing a NullPointerException if there is no address associated with the user object. This way, a sensible default value can be displayed without having to worry about exceptions being thrown by the JSP page.
City: <c:out value=”${user.address.city}” default=”N/A”/>
<c:set> may also be used to set the property of a JavaBeans object, or add or set a specific element in a java.util.Map object. For example:.
<!-- set property in JavaBeans object -->
<c:set target="${cust.address}" property="city" value="${city}"/>
<!-- set/add element in Map object -->
<c:set target="${preferences}" property="color"
value="${param.color}"/>
<c:remove var="cachedResult" scope="application"/>
Finally, the <c:catch> action provides a complement to the JSP error page mechanism. It is meant to allow page authors to recover gracefully from error conditions that they can control. For example:
<c:catch var=”exception”>
<!-- Execution we can recover from if exception occurs -->
...
</c:catch>
<c:if test=”${exception != null}”>
Sorry. Processing could not be performed because...
</c:if>
<c:out>
Evaluates an expression and outputs the result of the evaluation to the current JspWriter object.
Syntax
Without a body
<c:out value=”value” [escapeXml=”{true|false}”]
[default=”defaultValue”] />
With a body
<c:out value=”value” [escapeXml=”{true|false}”]>
default value
</c:out>
Deterrmines whether characters <,>,&,’,” in the resulting string should be converted to their corresponding character entity codes. Default value is true.
Default value if the resulting value is null.
本文地址:http://com.8s8s.com/it/it16106.htm