Attributes Attribute name Java type Dynamic value accepted Description A custom pattern in the form accepted by java.util.Locale Yes A locale to be used instead of the default.. Synta
Trang 1For a number type, the format specification can be currency, percent, integer, or
the type of customized pattern described for <fmt:formatNumber>
<fmt:parseDate>
The <fmt:parseDate> action parses a date and time value formatted according to
locale-specific rules A custom pattern can be specified, or locale-dependent default patterns for one
or both of the date portion and the time portion can be selected
The locale used is the one explicitly specified or is taken from the locale configuration setting
or the locale from the localization context setting If none of these are set, the locale is
determined as the best match for the user preferences (Accept-Language request header)
Syntax 1: Without a body
<fmt:parseDate value="dateAndTime"
[var="var" [scope="page|request|session|application"]] />
Syntax 2: With a body
A custom pattern in the form accepted by
java.text.SimpleDateFormat; see fmt:formatDate
Trang 2dateStyle String Yes One of the predefined
locale-dependent date patterns
locale-dependent time patterns
The locale used is the one explicitly specified or is taken from the locale configuration setting
or the locale from the localization context setting If none of these are set, the locale is determined as the best match for the user preferences (Accept-Language request header)
Syntax 1: Without a body
<fmt:parseNumber value="number"
[pattern="pattern" | type="number|currency|percent"]
[parseLocale="locale"]
[integerOnly="true|false"]
[var="var" [scope="page|request|session|application"]] />
Syntax 1: With a body
Trang 3Attributes
Attribute
name Java type
Dynamic value accepted Description
A custom pattern in the form accepted by
java.util.Locale Yes A locale to be used instead of the default
integerOnly boolean Yes Set to true to parse only the integer
portion
var String No The variable name The type is Number
from ISO-8859-1 The encoding can be specified explicitly I can also be determined based on
the request Content-Type header, if any, or the encoding session variable set by all JSTL
actions that set the locale for a response (javax.servlet.jsp.jstl.fmt.request.charset)
Syntax 1: Without parameters
<fmt:requestEncoding [value="encodingName"] />
Attributes
Attribute name Java type Dynamic value accepted Description
Trang 4Example
<%
Sets the request encoding to the same encoding as was used for
the previous response in the current session
>
<fmt:requestEncoding />
<fmt:setBundle>
The <fmt:setBundle> action sets the localization context configuration variable, which
establishes the localization context for all other JSTL actions that rely on one and disables the
locale lookup based on user preferences Alternatively, the localization context may be saved
as a scoped variable and provided as explicit input to other actions
The localization context contains a locale and the best match for the specified resource
bundle The locale for the localization context is either the locale defined by the locale
configuration setting or the best match for the user preferences specified by the
Accept-Language HTTP request header
java.util.ResourceBundle for details
Trang 5<fmt:setLocale>
The <fmt:setLocale> action sets the locale configuration variable, establishing the locale for all other JSTL actions that rely on a locale, disabling the locale lookup based on user preferences
Description
java.util.Locale Yes
The locale If set as a String, it must
be an ISO-639 language code, optionally followed by an ISO-3166 country code and separated by a hyphen
or an underscore character
java.util.Locale for details
scope String No The configuration variable scope
Syntax
<fmt:setTimeZone value="timeZone"
[var="var"] [scope="page|request|session|application"] />
Trang 6Attributes
Attribute
name Java type
Dynamic value accepted Description
java.util.TimeZone Yes
The time zone If specified as a String, it's an abbreviation, a name, or a GMT offset; see java.util.TimeZone for details
The variable name If omitted, the action sets the time-zone context configuration variable The type is
The time zone If specified as a
String, it's an abbreviation, a name, or a GMT offset; see
java.util.TimeZone for details
Example
<fmt:timeZone value="America/Los Angeles">
<fmt:formatDate value="${now}" type="both" />
</fmt:timeZone>
Trang 7B.4 Database Access Actions
<sql:dateParam>
The <sql:dateParam> action is used as a nested action for <sql:query> and
<sql:update> update to supply a date and time value for a value placeholder If a nullvalue is provided, the value is set to SQL NULL for the placeholder To ensure portability between different database engines, this action must be used when setting values for DATE,
TIME, and TIMESTAMP columns
The value must be of type java.util.Date or one of the SQL specific subclasses:
java.sql.Date, java.sql.Time, or java.sql.Timestamp If it's a
java.util.Date, the action converts it to the specified subclass
Syntax
<sql:dateParam value="parameterValue"
[type="timestamp|time|date"] />
Attributes
Attributename Java type Dynamic valueaccepted Description
value java.util.Date Yes The parameter value
type String Yes The SQL type the value should be converted to, if needed
The <sql:param> action is used as a nested action for <sql:query> and
<sql:update> to supply a value for a value placeholder If a null value is provided, the value is set to SQL NULL for the placeholder To ensure portability between different database engines, the value type must be a supported type for the target column, for instance a numeric value for an INT or FLOAT column, and a String value for a CHAR or VARCHAR
Trang 8column See <sql:dateParam> for setting values for DATE, TIME, and TIMESTAMPcolumns
Syntax 1: Without a body
<sql:param value="parameterValue" />
Syntax 2: With a body
<sql:param>
parameterValue
</sql:param>
Attributes
Attribute name Java type Dynamic value accepted Description
The action uses a connection provided by an <sql:transaction> action, or
transaction action or if not part of a transaction from the explicitly specified data source or the data-source configuration setting The number of rows to retrieve can be limited explicitly or with the maximum rows configuration setting
Syntax 1: Without a body
Trang 9Syntax 2: With a body for parameters
The data source to use If specified as a
String, it can be either a JNDI path or a list
of JDBC parameters as described for the datasource configuration setting
maxRows int Yes The maximum number of rows to include in theresult If omitted or -1, all rows are included
startRow int Yes The 0-based index for the first row to include inthe result
Trang 10<sql:setDataSource>
The <sql:setDataSource> action sets the data source configuration variable or saves the data-source information in a scoped variable that can be used as input to the other JSTL database actions
This action is primarily intended for prototyping and small, simple applications See Chapter 11 and Chapter 23 for alternative ways to make a data source available
The variable name If omitted, the data-source configuration variable is set The type is either
javax.sql.DataSource or
String
scope String No The variable or configuration variable scope
Trang 11The <sql:transaction> action establishes a transaction context for a set of
<sql:query> and <sql:update> actions It ensures that the database modifications performed by the nested actions are either committed or rolled back if an exception is thrown
by any nested action
The action provides a connection to the nested database actions, either from the explicitly specified data source or from the data-source configuration setting
The transaction isolation level If omitted, the isolation level the data source has been configured with is used
Trang 12The <sql:update> action executes an SQL statement that updates the database, such as an
INSERT, UPDATE, or DELETE statement, and optionally saves the number of affected rows in
a scoped variable SQL DDL statements, such as CREATE TABLE, can also be executed with this action The statement may contain question marks as placeholders for values assigned by nested <sql:param> actions
The action uses a connection provided by an <sql:transaction> action, or if not part of
a transaction, from the explicitly specified data source or the data-source configuration setting
Syntax 1: Without a body
<sql:update sql="sqlStatement"
[dataSource="dataSource"]
[var="var" [scope="page|request|session|application"]] />
Syntax 2: With a body for parameters
Trang 13Syntax 3: With a body for the statement and parameters
Description
javax.sql.DataSourceYes
The data source to use If specified as a
String, it can be either a JNDI path or a list
of JDBC parameters as described for the source configuration setting
The <x:choose> action controls the processing of nested <x:when> and
<x:otherwise> actions It allows only the first <x:when> action with a test expression
that evaluates to true to be processed, or gives the go-ahead to the single <x:otherwise>
action if none do
Syntax
<x:choose>
<x:when> actions and optionally one <x:otherwise> action
</x:choose>
Trang 14var String No The name of the nested variable holding the
Trang 15<x:if>
The <x:if> action evaluates its body only if the specified XPath expression evaluates to
true Alternatively, the evaluation result can be saved as a scoped Boolean variable
Syntax 1: Without a body
<x:if select="booleanXPathExpression"
var="var" [scope="page|request|session|application"]/>
Syntax 1: With a body
<x:if select="booleanXPathExpression">
Dynamic value
select String No The XPath expression to evaluate
Syntax
<x:otherwise>
JSP elements
</x:otherwise>
Trang 16select String No The XPath expression to evaluate
escapeXml boolean Yes If true, the characters < > & ' " are converted to
the corresponding XML character entity codes
Trang 17Attributes
Attribute name Java type Dynamic value accepted Description
Example
<x:transform xml="${doc}" xslt="${stylesheet}">
<x:param name="custId" value="${param.id}" />
</x:transform>
<x:parse>
The <x:parse> action parses an XML document and saves the result either as a standard
org.w3c.dom.Document object or as an implementation-dependent object
Syntax 1: Without a body
[var="var" [scope="page|request|session|application"] |
varDom="var" [scopeDom="page|request|session|application"]]
xml String or Reader Yes The XML document
A URI to use as a base for relative references in the document, as an absolute URL, or as a page- or context-relative path
filter org.xml.sax.XMLFilterYes A filter that can remove elements that
aren't of interest
Trang 18var String No The variable name The type is
implementation-dependent
<x:set select="XPathExpression"
var="var" [scope="page|request|session|application"] />
Attributes
Attribute
name Java type Dynamic value accepted Description
select String No The XPath expression to evaluate
The variable name The type is the type of the expression evaluation, converted to the corresponding Java type
scope String No The variable scope
Trang 19captured or processed further by a javax.xml.transform.Result object Nested
<x:param> actions can supply values for parameters used in the stylesheet
Syntax 1: Without a body
<x:transform xml="XMLDocument" xslt="XSLTStylesheet"
[xmlSystemId="systemId"] [xsltSystemId="systemId"]
[var="var" [scope="page|request|session|application"] |
result="resultObject"] />
Syntax 2: With a body for parameter values
<x:transform xml="XMLDocument" xslt="XSLTStylesheet"
[xmlSystemId="systemId"] [xsltSystemId="systemId"]
[var="var" [scope="page|request|session|application"] |
xml
String, Reader,
javax.transformation.Source,
org.w3c.dom.Document, or object exposed by x:parse or x:set
Yes The XML document
javax.transformation.Source Yes The XSLT stylesheet
A URI to use as a base for relative references in the document, as an absolute URL or as a page- or context-relative path
A URI to use as a base for relative references in the stylesheet, as an absolute URL or as a page- or context-relative path
Trang 20result javax.xml.transform.Result Yes Object that captures or
processes the result
The <x:when> action represents one of the mutually exclusive alternatives within a
<x:choose> block It evaluates its body only if it's the first <x:when> action in the block with a test expression that evaluates to true
Attribute name Java type Dynamic value accepted Description
Trang 21B.6 Support and Utility Types
Returns the value of test condition This method must be implemented by the subclass
public void setScope(String scope)
Sets the scope attribute value
public void setVar(String var)
Sets the var attribute value
Trang 22public static String getLocalizedMessage(javax.servlet.jsp.PageContext p,
String key, String basename)
Returns the message matching the key from the specified resource bundle and the locale specified by the locale context setting or the localization context setting, or ???key??? if the key isn't found
public static String getLocalizedMessage(javax.servlet.jsp.PageContext p,
String key, Object[] params)
Returns the parameterized message matching the key from the resource bundle and locale specified by the localization context setting, or ???key??? if the key isn't found
public static String getLocalizedMessage(javax.servlet.jsp.PageContext p,
String key, Object[] params, String basename)
Returns the parameterized message matching the key from the specified resource bundle and the locale specified by the locale context setting or the localization context setting, or
???key??? if the key isn't found
Creates an empty context
public LocalizationContext(java.util.ResourceBundle bundle)
Creates a context with the specified resource bundle but no locale
Trang 23public LocalizationContext(java.util.ResourceBundle bundle,
java.util.Locale locale)
Creates a context with the specified resource bundle and locale
Methods
public java.util.ResourceBundle getResourceBundle( )
Returns the resource bundle
public java.util.Locale getLocale( )
Returns the locale
LoopTag
The LoopTag interface is implemented by tag handlers for iteration actions, such as the
<c:forEach> action Its methods provide access to the current iteration element and the iteration status
public Object getCurrent( )
Returns the current iteration element
public javax.servlet.jsp.jstl.core.LoopTagStatus getLoopStatus( )
Returns the iteration status
Trang 24public Integer getBegin( )
Returns the begin attribute value for the associated action element or null if not specified
public int getCount( )
Returns the current 1-based iteration count
public Object getCurrent( )
Returns the current iteration element
public int getIndex( )
Returns the current 0-based iteration index
public Integer getStart( )
Returns the start attribute value for the associated action element or null if not specified
public Integer getStep( )
Returns the step attribute value for the associated action element or null if not specified
public boolean isFirst( )
Returns true for the first iteration round
public boolean isLast( )
Returns true for the last iteration round
Trang 25protected int begin
protected boolean beginSpecified
protected int end
protected boolean endSpecified
protected int itemId
protected int statusId
protected int step
protected boolean stepSpecified
Methods
public void doAfterBody( ) throws javax.servlet.jsp.tagext.JspTagException
Prepares for the next iteration until hasNext( ) returns false
public void doCatch(Throwable t) throws Throwable
Rethrows the exception
public void doFinally( ) throws javax.servlet.jsp.tagext.JspTagException
Removes the nested variables
public void doStartTag( ) throws javax.servlet.jsp.tagext.JspTagException
Prepares for the iteration
protected abstract Object getCurrent( )
Returns the current iteration element
public javax.servlet.jsp.jstl.core.LoopTagStatus getLoopStatus( )
Returns the iteration status
protected abstract boolean hasNext( )
throws javax.servlet.jsp.tagext.JspTagException
Trang 26Returns true if there are more iteration elements
protected abstract Object next( )
throws javax.servlet.jsp.tagext.JspTagException
Returns the next iteration element
protected abstract void prepare( )
throws javax.servlet.jsp.tagext.JspTagException
Prepares for the iteration
public void setScope(String scope)
Sets the scope attribute value
public void setVar(String var)
Sets the var attribute value
protected void validateBegin( )
throws javax.servlet.jsp.tagext.JspTagException
Throws an exception if the begin attribute has an invalid value
protected void validateEnd( )
throws javax.servlet.jsp.tagext.JspTagException
Throws an exception if the end attribute has an invalid value
protected void validateStep( )
throws javax.servlet.jsp.tagext.JspTagException
Throws an exception if the end attribute has an invalid value
The LoopTagSupport class can be extended by a tag handler for a custom iteration action
Trang 27Methods
public java.util.SortedMap[] getRows( )
Returns an array of SortedMap objects Each array element represents a row, and the map contains the column values, with the column name as a case-insensitive key public Object[][] getRowsByIndex( )
Returns an array of arrays The first array dimension represents rows, and the second represents the column values
public String[] getColumnNames( )
Returns an array of String objects, representing the column values in the same order
as in the arrays returned by getRowsByIndex( )
public boolean isLimitedByMaxRows( )
Returns true if the result was limited by the maximum row configuration setting or action element attributes
Returns a Result object with the data from the specified ResultSet
public static javax.servlet.jsp.jstl.sql.Result
toResult(java.sql.ResultSet rs, int maxRows)
Returns a Result object with the data from the specified ResultSet, up to the specified number of maximum rows
Trang 28SQLExecutionTag
The SQLExecutionTag interface is implemented by the <sql:query> and
<sql:update> tag handlers so that they can receive placeholder parameter values from
public void addSQLParameter(Object value)
Adds a parameter value to use with
java.sql.PreparedStatement.setObject( )
B.7 Configuration Settings
Data Source
The data-source configuration setting can be set as a String in this format, in which optional
parts are embedded in brackets:
url [, [driver] [, [user] [, [password]]]
This type of value creates a simple DataSource without any pooling capabilities and is
intended only for prototype and low-end applications, as described in Chapter 11 It can also
be set to a JNDI path for a DataSource made available by the container, or to a
DataSource created by custom code, such as a servlet or listener These options are
described in detail in Chapter 23
Details
Variable name: javax.servlet.jsp.jstl.sql.dataSource
Java constant: Config.SQL_DATA_SOURCE
Java type: String or javax.sql.DataSource
Set by: <sql:setDataSource>, context parameter, or custom code
Trang 29Used by: <sql:query>, <sql:update>, and <sql:transaction>
Fallback Locale
Setting the fallback locale configuration setting provides a default locale to be used when the lookup of a locale based on user preferences (passed through the Accept-Language header value) fails to match an available locale When a String value is used to set these two variables, it must be specified as a two-letter lowercase ISO-639 language code, optionally followed by a two-letter uppercase ISO-3166 country code, separated by a hyphen or an underscore character
Java type: String or java.util.Locale
Set by: Context parameter or custom code
Used by: <fmt:bundle>
Java type: String or java.util.Locale
Set by: <fmt:setLocale>, context parameter, or custom code
Trang 30Used by: <fmt:bundle>
LocalizationContext class, which contains references to both a locale and a resource bundle for a locale
<fmt:parseNumber>,<fmt:formatDate>, and <fmt:parseDate>
Set by: Context parameter or custom code
Used by: <sql:query>
Trang 31Time Zone
The time zone configuration setting provides a default time zone for the JSTL actions formatting and parsing dates String values for the time-zone setting must be of the type defined for the java.util.TimeZone class: an abbreviation, a full name, or a GMT offset
Details
Variable name: javax.servlet.jsp.jstl.fmt.timeZone
Java constant: Config.FMT_TIME_ZONE
Java type: String or java.util.TimeZone
Set by: <fmt:setTimeZone>, context parameter, or custom code
Used by: <fmt:formatDate> and <fmt:parseDate>