- JavaServer Pages標準標記庫(JSTL)是一個有用的JSP標籤的集合,它封裝了許多JSP應用程序通用的核心功能。
JavaServer Pages標準標記庫(JSTL)是一個有用的JSP標籤的集合,它封裝了許多JSP應用程序通用的核心功能。
JSTL支持常見的,結構性任務,如迭代和條件,標籤爲操縱XML文件,國際化標籤和SQL標籤。它還提供了一個框架,將現有的自定義標籤和JSTL標籤。
JSTL標籤進行分類,根據其功能分爲以下,可用於創建一個JSP頁面時,JSTL標籤庫組:
-
核心標籤(Core Tags)
-
格式化標籤(Formatting tags)
-
SQL標籤(SQL tags)
-
XML標籤(XML tags)
-
JSTL函數(JSTL Functions)
安裝JSTL庫:
如果您使用的是Apache Tomcat容器,那麼按照下面兩個簡單的步驟:
-
下載的二進制發行版,從Apache標準標記庫和解壓的壓縮文件。
-
使用標準的標籤庫從Jakarta Taglibs分佈,複製發行版'lib'目錄到應用程序的JAR文件 webapps\ROOT\WEB-INF\lib 目錄中。
要使用JSTL庫,要在每一個JSP的頂部,使用該庫必須包括一個<taglib>指令。
核心標籤:
核心組的標籤是最常用的JSTL標籤。以下是在您的JSP的語法包括JSTL核心庫:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
有以下核心JSTL標籤:
Tag | Description |
---|---|
<c:out > | 和<%= ... >標籤一樣功能, 但對於表達式。 |
<c:set > | 設置表達式求值的結果在一個'範圍' |
<c:remove > | 刪除一個範圍的變量(從一個特定的範圍內,如果已指定)。 |
<c:catch> | Catches any Throwable that occurs in its body and optionally exposes it. |
<c:if> | Simple conditional tag which evalutes its body if the supplied condition is true. |
<c:choose> | Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> |
<c:when> | Subtag of <choose> that includes its body if its condition evalutes to 'true'. |
<c:otherwise > | Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'. |
<c:import> | Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. |
<c:forEach > | The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality . |
<c:forTokens> | Iterates over tokens, separated by the supplied delimeters. |
<c:param> | Adds a parameter to a containing 'import' tag's URL. |
<c:redirect > | Redirects to a new URL. |
<c:url> | Creates a URL with optional query parameters |
格式化標籤:
使用JSTL格式標籤來格式化和顯示文本,日期,時間和數字的,國際化的網站。以下是在您的JSP的語法,包括格式化庫:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
以下是格式化JSTL標籤列表:
Tag | Description |
---|---|
<fmt:formatNumber> | To render numerical value with specific precision or format. |
<fmt:parseNumber> | Parses the string representation of a number, currency, or percentage. |
<fmt:formatDate> | Formats a date and/or time using the supplied styles and pattern |
<fmt:parseDate> | Parses the string representation of a date and/or time |
<fmt:bundle> | Loads a resource bundle to be used by its tag body. |
<fmt:setLocale> | Stores the given locale in the locale configuration variable. |
<fmt:setBundle> | Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable. |
<fmt:timeZone> | Specifies the time zone for any time formatting or parsing actions nested in its body. |
<fmt:setTimeZone> | Stores the given time zone in the time zone configuration variable |
<fmt:message> | To display an internationalized message. |
<fmt:requestEncoding> | Sets the request character encoding |
SQL標籤:
JSTL的SQL標籤庫標籤可以交互關係型數據庫(如Oracle,MySQL或Microsoft SQL Server的關係數據庫管理系統)。
以下是在您的JSP語法包括JSTL SQL庫:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
以下是SQL JSTL標籤的列表:
Tag | Description |
---|---|
<sql:setDataSource> | Creates a simple DataSource suitable only for prototyping |
<sql:query> | Executes the SQL query defined in its body or through the sql attribute. |
<sql:update> | Executes the SQL update defined in its body or through the sql attribute. |
<sql:param> | Sets a parameter in an SQL statement to the specified value. |
<sql:dateParam> | Sets a parameter in an SQL statement to the specified java.util.Date value. |
<sql:transaction > | Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction. |
XML 標籤:
JSTL XML標記提供了一種創建和操作XML文檔的JSP爲中心的方式。以下是在您的JSP的語法包括JSTL XML庫。
JSTL XML標記庫具有自定義標籤與XML數據交互。這包括XML解析,轉換XML數據,流控制,基於XPath表達式。
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
在繼續之前的例子中,將需要複製下面的兩個XML和XPath相關的庫到<Tomcat安裝目錄> 的\lib目錄:
-
XercesImpl.jar: 下載地址 http://www.apache.org/dist/xerces/j/
-
xalan.jar: 下載地址 http://xml.apache.org/xalan-j/index.html
以下是XML JSTL標籤列表:
Tag | Description |
---|---|
<x:out> | Like <%= ... >, but for XPath expressions. |
<x:parse> | Use to parse XML data specified either via an attribute or in the tag body. |
<x:set > | Sets a variable to the value of an XPath expression. |
<x:if > | Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored. |
<x:forEach> | To loop over nodes in an XML document. |
<x:choose> | Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> |
<x:when > | Subtag of <choose> that includes its body if its expression evalutes to 'true' |
<x:otherwise > | Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false' |
<x:transform > | Applies an XSL transformation on a XML document |
<x:param > | Use along with the transform tag to set a parameter in the XSLT stylesheet |
JSTL 函數:
JSTL包括一些標準功能,其中大部分是常見的字符串操作函數。以下是在JSP的語法包函JSTL函數庫:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
以下是JSTL函數列表:
Function | Description |
---|---|
fn:contains() | Tests if an input string contains the specified substring. |
fn:containsIgnoreCase() | Tests if an input string contains the specified substring in a case insensitive way. |
fn:endsWith() | Tests if an input string ends with the specified suffix. |
fn:escapeXml() | Escapes characters that could be interpreted as XML markup. |
fn:indexOf() | Returns the index withing a string of the first occurrence of a specified substring. |
fn:join() | Joins all elements of an array into a string. |
fn:length() | Returns the number of items in a collection, or the number of characters in a string. |
fn:replace() | Returns a string resulting from replacing in an input string all occurrences with a given string. |
fn:split() | Splits a string into an array of substrings. |
fn:startsWith() | Tests if an input string starts with the specified prefix. |
fn:substring() | Returns a subset of a string. |
fn:substringAfter() | Returns a subset of a string following a specific substring. |
fn:substringBefore() | Returns a subset of a string before a specific substring. |
fn:toLowerCase() | Converts all of the characters of a string to lower case. |
fn:toUpperCase() | Converts all of the characters of a string to upper case. |
fn:trim() |
Removes white spaces from both ends of a string. |
<%@ taglib %>引入標籤庫
=========================================================================
1、以classPath中,加入jar包: standard-1.1.2.jar , jstl-1.1.2.jar
2、在相目\WEB-INF\tld\文件夾中放入常用的tld文件:c.tld,fmt.tld
3、在jsp文件的頂部加入以下內容:
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<c:> 核心標籤庫
==========================================================================
JSTL 核心標籤庫(C標籤)標籤共有13個,功能上分爲4類:
1.表達式控制標籤:out、set、remove、catch
2.流程控制標籤:if、choose、when、otherwise
3.循環標籤:forEach、forTokens
4.URL操作標籤:import、url、redirect
<c:forEach> 標籤
-------------------------------------------------------------------
爲循環控制,它可以將集合(Collection)中的成員循序瀏覽一遍。
<c:forEach> 標籤的語法 說明 :
語法1:迭代一集合對象之所有成員
- <c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
- 本體內容
- </c:forEach>
<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
本體內容
</c:forEach>
語法2:迭代指定的次數
- <c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin" end="end" [step="step"]>
- 本體內容
- </c:forEach>
<c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin" end="end" [step="step"]>
本體內容
</c:forEach>
<c:forEach> 標籤的 屬性說明 :
<c:forEach> 標籤的 屬性 : varStatus屬性: 它的提供另外四個屬性:index,count,fist和last,它們個自的意義如下:
- 屬性 類型 意義
- index number 現在指到成員的索引
- count number 總共指到成員的總和
- first boolean 現在指到成員是否爲第一個
- last boolean 現在指到成員是否爲最後一個
<c:forEach> 遍歷 List列表:
對於一個基本類型的數組,當前元素將作爲相應包裝類(Integer、Float等等)的一個實例提供。
- <c:forEach items="${domainList }" var="item">
- <tr>
- <td align="center" valign="middle">${item["domain"]==null?" ":item["domain"]}</td>
- <td align="center" valign="middle"><fmt:formatDate value="${item['bind_date']}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
- <td align="center" valign="middle">
- <c:if test="${item['domain']!=null}">
- <a href="javascript:;" id="${item['domain']}" class="del"> </a>
- </c:if>
- </td>
- </tr>
- </c:forEach>
<c:forEach items="${domainList }" var="item">
<tr>
<td align="center" valign="middle">${item["domain"]==null?" ":item["domain"]}</td>
<td align="center" valign="middle"><fmt:formatDate value="${item['bind_date']}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td align="center" valign="middle">
<c:if test="${item['domain']!=null}">
<a href="javascript:;" id="${item['domain']}" class="del"> </a>
</c:if>
</td>
</tr>
</c:forEach>
<c:forEach> 遍歷Map:
對於一個java.util.Map,當前元素則作爲一個java.util.Map.Entry提供。
- <c:if test="${!empty permissionMap}">
- <c:forEach items="${permissionMap}" var="item">
- <tr>
- <td>${item.value.id}</td>
- <td>${item.value.urlOnClass}</td>
- <td>${item.value.urlOnMethod}</td>
- </tr>
- </c:forEach>
- </c:if>
<c:if test="${!empty permissionMap}">
<c:forEach items="${permissionMap}" var="item">
<tr>
<td>${item.value.id}</td>
<td>${item.value.urlOnClass}</td>
<td>${item.value.urlOnMethod}</td>
</tr>
</c:forEach>
</c:if>
<c:forTokens> 標籤
-------------------------------------------------------------------
用來瀏覽一字符串中所有的成員,其成員是由定義符號(delimiters)所分隔的。
<c:forTokens> 標籤的語法 說明 :
- <c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"]
- [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
- 本體內容
- </c:forTokens>
<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"]
[varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
本體內容
</c:forTokens>
<c:forTokens> 標籤的 屬性說明 :
<c:out> 標籤
-------------------------------------------------------------------
主要用來顯示數據的內容
<c:out> 標籤的語法 說明 :
語法1:沒有本體(body)內容
語法2:有本體內容
default value
</c:out>
<c:forEach> 標籤的 屬性說明 : 略
一般來說,<c:out>默認會將<、>、’、” 和 & 轉換爲 <、>、'、" 和&。假若不想轉換時,只需要設定<c:out>的escapeXml屬性爲fasle就可以了。
<c:set> 標籤
-------------------------------------------------------------------
主要用來將變量儲存至JSP範圍中或是JavaBean的屬性中。
<c:set> 標籤的語法 說明 :
語法1:將value的值儲存至範圍爲scope的 varName 變量之中
語法2:將本體內容的數據儲存至範圍爲scope的 varName 變量之中
… 本體內容
</c:set>
語法3:將 value的值儲存至 target 對象的屬性中
語法4:將本體內容的數據儲存至target 對象的屬性中
… 本體內容
</c:set>
<c:set> 標籤的 屬性說明 :
<c:remove> 標籤
-------------------------------------------------------------------
主要用來移除變量。
<c:remove> 標籤的語法 說明 :
<c:remove var="varName" [scope="{ age|request|session|application }"] />
<c:catch> 標籤
-------------------------------------------------------------------
主要用來處理產生錯誤的異常狀況,並且將錯誤信息儲存起來。
<c:catch> 標籤的語法 說明 :
<c:catch [var="varName"] >… 欲抓取錯誤的部分</c:catch>
<c:if> 標籤
-------------------------------------------------------------------
的用途就和我們一般在程序中用的if一樣。
<c:if> 標籤的語法 說明 :
語法1:沒有本體內容(body)
<c:if test="testCondition" var="varName" [scope="{page|request|session|application}"]/>
語法2:有本體內容
<c:if test="testCondition" [var="varName"] [scope="{page|request|session|application}"]>本體內容</c:if>
示例:
<c:if test="${not empty item.publish_time}">內容</c:if>
<c:if test="${item['domain']!=null}">內容</c:if>
<c:if test="${!empty permissionMap}"> 內容</c:if>
c:choose> <c:when> <c:otherwise> 標籤
-------------------------------------------------------------------
<c:choose when otherwise> 標籤的語法 說明 :
- <c:set var="score">85</c:set>
- <c:choose>
- <c:when test="${score>=90}">
- 你的成績爲優秀!
- </c:when>
- <c:when test="${score>=70&&score<90}">
- 您的成績爲良好!
- </c:when>
- <c:when test="${score>60&&score<70}">
- 您的成績爲及格
- </c:when>
- <c:otherwise>
- 對不起,您沒有通過考試!
- </c:otherwise>
- </c:choose>
<c:set var="score">85</c:set>
<c:choose>
<c:when test="${score>=90}">
你的成績爲優秀!
</c:when>
<c:when test="${score>=70&&score<90}">
您的成績爲良好!
</c:when>
<c:when test="${score>60&&score<70}">
您的成績爲及格
</c:when>
<c:otherwise>
對不起,您沒有通過考試!
</c:otherwise>
</c:choose>
<fmt:> 格式 化標籤庫
==========================================================================
一:JSTL格式化標籤又稱爲I18N標籤庫,主要用來編寫國際化的WEB應用,使用此功能可以對一個特定的語言請求做出合適的處理。
例如:中國內地用戶將顯示簡體中文,臺灣地區則顯示繁體中文,使用I18N格式化標籤庫還可以格式化數字和日期,例如同一數字或日趨,在不同國家可能有不同的格式,使用I18N格式標籤庫可以將數字和日期格式爲當地的格式。
在JSP頁面中要使用到格式化標籤,需要引入下面的語句:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"% >
二:概覽
-------------------------------------------------------------------
格式化標籤
<fmt:fromatNumber>
<fmt:formatDate>
<fmt:parseDate>
<fmt:parseNumber>
<fmt:setTimeZone>
<fmt:timeZone>
國際化標籤
<fmt:setLocale>
<fmt:requestEncoding>
<fmt:bundle>
<fmt:message>
<fmt:param>
<fmt:setBundle>
三:<fmt:formatNumber>
-------------------------------------------------------------------
此標籤會根據區域定製的方式將數字格式化成數字,貨幣,百分比。
此標籤的屬性:
value:要格式化的數字
type:按照什麼類型格式化
pattern:自定義格式化樣式
currencyCode:ISO-4721貨幣代碼,只適用於按照貨幣格式化的數字
currencySymbol: 貨幣符號,如¥,只適用於按照貨幣格式化的數字
groupingUsed: 是否包含分隔符
maxIntegerDigits: 整數部分最多顯示多少位
mixIntegerDigits: 整數部分最少顯示多少位
maxFractionDigits: 小數部分最多顯示多位位
minFractionDigits: 小數部分最少顯示多位位
var:存儲格式化後的結果
scope: 存儲的範圍
示例1:
- <c:set var="score">85</c:set>
- <c:choose>
- <c:when test="${score>=90}">
- 你的成績爲優秀!
- </c:when>
- <c:when test="${score>=70&&score<90}">
- 您的成績爲良好!
- </c:when>
- <c:when test="${score>60&&score<70}">
- 您的成績爲及格
- </c:when>
- <c:otherwise>
- 對不起,您沒有通過考試!
- </c:otherwise>
- </c:choose>
- <c:set var="score">85</c:set>
- <c:choose>
- <c:when test="${score>=90}">
- 你的成績爲優秀!
- </c:when>
- <c:when test="${score>=70&&score<90}">
- 您的成績爲良好!
- </c:when>
- <c:when test="${score>60&&score<70}">
- 您的成績爲及格
- </c:when>
- <c:otherwise>
- 對不起,您沒有通過考試!
- </c:otherwise>
- </c:choose>
<c:set var="score">85</c:set>
<c:choose>
<c:when test="${score>=90}">
你的成績爲優秀!
</c:when>
<c:when test="${score>=70&&score<90}">
您的成績爲良好!
</c:when>
<c:when test="${score>60&&score<70}">
您的成績爲及格
</c:when>
<c:otherwise>
對不起,您沒有通過考試!
</c:otherwise>
</c:choose>
<c:set var="score">85</c:set>
<c:choose>
<c:when test="${score>=90}">
你的成績爲優秀!
</c:when>
<c:when test="${score>=70&&score<90}">
您的成績爲良好!
</c:when>
<c:when test="${score>60&&score<70}">
您的成績爲及格
</c:when>
<c:otherwise>
對不起,您沒有通過考試!
</c:otherwise>
</c:choose>
注意:如果要實現國際化,那麼編碼格式要設置爲utf-8.
從程序運行效果可以看出,設定的區域不同,格式化數字的顯示也會不同.
四:type屬性:可以是數字(number),貨幣(currency),百分比(percent)
示例2:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="number"/><br />
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:formatNumber value="0.3" type="percent"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="number"/><br />
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:formatNumber value="0.3" type="percent"/><br />
- </div>
- </div>
- </body>
- </html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="number"/><br />
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:formatNumber value="0.3" type="percent"/><br />
</div>
</div>
</body>
</html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="number"/><br />
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:formatNumber value="0.3" type="percent"/><br />
</div>
</div>
</body>
</html>
currencyCode爲貨幣代碼,例如美元爲USD,人民幣爲CNY等
currencySymbol爲貨幣符號例如,人民幣爲¥,美元爲$。
如果不指定區域,則會根據語言區域自動選擇currencySymbol
示例3:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:setLocale value="en_Us"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
</div>
</div>
</body>
</html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:setLocale value="en_Us"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
</div>
</div>
</body>
</html>
currencySymbol屬性還可以自定義要顯示的頭標識,但是一定得type="currency"纔會生效,例如:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>chapter4.jsp</title>
- </head>
- <body>
- <div>
- <div>
- <fmt:setLocale value="zh_cn"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- <fmt:setLocale value="en_Us"/>
- <fmt:formatNumber value="0.3" type="currency"/><br />
- </div>
- </div>
- </body>
- </html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:setLocale value="en_Us"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
</div>
</div>
</body>
</html>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<fmt:setLocale value="zh_cn"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
<fmt:setLocale value="en_Us"/>
<fmt:formatNumber value="0.3" type="currency"/><br />
</div>
</div>
</body>
</html>
自定義數字樣式
<fmt:formatNumber value="12.31" pattern=".0000"/><br/>
<fmt:formatNumber value="1234" pattern="###.##E0"/>
會顯示:
12.3100
1.234E3
會四捨五入
var:定義一個變量,存儲格式化後的結果,scope指定變量存儲的範圍.用法和前面講的標籤一致.
五:<fmt:parseNumber>
-------------------------------------------------------------------
此標籤用來將字符串類型的數字,貨幣或百分比轉換成數字類型,和<fmt:formatNumber>標籤的作用正好相反.
value: 要轉換的字符串
type: 指定要轉換的字符串爲什麼類型,可取值:number,percent,currency
pattern: 自定義格式化樣式
parseLocale: 指定區域來轉換字符串
IntegerOnly: 轉換後的數字是否只顯示整數部分
var: 存儲轉換後的結果
scope: 存儲的範圍
示例1:
<fmt:parseNumber value="500,800,200"/>
顯示: 500800200
示例2:
<fmt:parseNumber value="52%" type="percent" />
顯示: 0.52 (52%在這裏是一個字符串, type指定這個字符串是什麼類型的值)
示例3:
<fmt:parseNumber value="¥123" type="currency" />
顯示123, ¥123在這裏是一個字符串, type指定這個字符串是什麼類型的值
示例4:
<fmt:parseNumber value="123.333" type="number" /><br/>
<fmt:parseNumber value="123.333" type="number" integerOnly="true"/><br/>
顯示:
123.333
123
integerOnly確定是否只顯示整數部分.
示例5:
<fmt:parseNumber value="¥123.333" type="currency" parseLocale="zh_CN"/><br/>
<fmt:parseNumber value="$123.333" type="currency" parseLocale="en_US"/><br/>
parseLocale="en_US"主要是配合當type="currency"時用的,
如果要轉換貨幣的字符串類型爲value="¥123.333",不設置語言環境的話,會取當前瀏覽器的默認設置,否則就要加上parseLocale="zh_CN",指定環境爲中文環境
如果要轉換貨幣的字符串類型爲value="$123.333",不設置語言環境的話,會取當前瀏覽器的默認設置,如果默認爲zh_cn的話,程序會報錯的,否則就要加上parseLocale="en_US",指定環境爲英文美國環境
六:<fmt:formatDate />
-------------------------------------------------------------------
此標籤可以將日期格式化.
屬性介紹:
value 用來格式化的時間或日期
type 指定格式化的是日期還是時間,或者兩者都是取值範圍:date,time,both
pattern 自定義格式化樣式
dateStyle 日期的格式化樣式
timeStyle 時間的格式化樣式
timeZone 指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
自定義格式:
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
- <br />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
- <br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
<br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
<br />
注意這裏小時 hh表示12小時制, HH代表24小時制
示例1:
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
- <br />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
- <br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
<br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy/MM/dd hh:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yyyy年MM月dd日 hh小時mm分鐘ss秒" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" pattern="yy/MM/dd hh:mm:ss" />
<br />
大家可以看到大陸和臺灣顯示日期的格式是有區別的.
顯示結果:
2009-12-7
2009/12/7
示例2:
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="time"/>
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" type="time"/>
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="time"/>
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" type="time"/>
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="time"/>
顯示結果:
14:59:28
下午 02:59:28
type可取值及意義:
date 格式化日期
time格式化時間
both格式化日期時間
示例3:
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" />
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" type="both" />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" />
- <br />
- <fmt:setLocale value="zh_tw" />
- <fmt:formatDate value="<%=new Date()%>" type="both" />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
<br />
<fmt:setLocale value="zh_tw" />
<fmt:formatDate value="<%=new Date()%>" type="both" />
輸出結果:
2009-12-7 21:24:26
2009/12/7 下午 09:24:26
dateStyle用來設定日期顯示的樣式,其值可以是default, short, medium, long, full,請看示例:
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="default" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="short" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="medium" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="long" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="full" />
- <br />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="default" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="short" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="medium" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="long" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="full" />
- <br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="full" />
<br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" dateStyle="full" />
<br />
顯示結果如下:
2009-12-7 21:30:49
09-12-7 21:30:49
2009-12-7 21:30:49
2009年12月7日 21:30:49
2009年12月7日 星期一 21:30:49
可以看到dateStyle屬性只對日期部分起作用,時間部分沒有作用.
timeStyle用來顯示時間部分的樣式,取值範圍同上
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="default" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="short" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="medium" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="long" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="full" />
- <br />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="default" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="short" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="medium" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="long" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="full" />
- <br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="full" />
<br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="default" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="short" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="medium" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="long" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="both" timeStyle="full" />
<br />
輸出:
2009-12-7 21:35:52
2009-12-7 下午9:35
2009-12-7 21:35:52
2009-12-7 下午09時35分52秒
2009-12-7 下午09時35分52秒 CST
timeZone用來設定時區,時區的意思類似於酒店裏大堂放的幾個時鐘,比如現在時間會有北京時間,東京時間,紐約時間,倫墩時間,
取值範圍爲:EST, CST, MST, PST
--------------------------
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="EST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="CST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="MST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="PST" />
- <br />
- <fmt:setLocale value="zh_cn" />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="EST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="CST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="MST" />
- <br />
- <fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="PST" />
- <br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="EST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="CST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="MST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="PST" />
<br />
<fmt:setLocale value="zh_cn" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="EST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="CST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="MST" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" timeZone="PST" />
<br />
輸出結果:
下午09時41分37秒 CST
上午08時41分37秒 EST
上午07時41分37秒 CST
上午06時41分37秒 MST
上午05時41分37秒 PST
七:<fmt:parseDate>
-------------------------------------------------------------------
將字符串類型的時間轉換爲日期類型.
value 用來格式化的時間或日期的字符串
type 指定格式化的是日期還是時間,或者兩者都是取值範圍:date,time,both
pattern 自定義格式化樣式
dateStyle 日期的格式化樣式
timeStyle 時間的格式化樣式
timeZone 指定使用的時區
var 存儲格式化後的結果
scope 指定存儲的範圍
示例:
<fmt:setLocale value="zh_cn" />
<fmt:parseDate type="date" value="2008-4-5"/>
輸出: Sat Apr 05 00:00:00 CST 2008,
這裏已經將字符串” 2008-4-5”轉換爲了日期對象了.轉換一定得注意,類似於2008-4-5這樣的字符串,type必須爲date,類似於12:34:56的字符串,type必須爲time類似於2008-4-5 12:34:56這樣的字符串,type必須爲both還要注意瀏覽器的語言環境的設置,如果爲zh_tw,那麼字符串就必須得符合當地的標準,如爲2009/12/7 下午 09:24:26就正確轉換爲日期對象,否則就會報錯.
八:<fmt:setTimeZone>
-------------------------------------------------------------------
value 設定時區
var 存儲設定的時區
scope 存儲的範圍
value用來設定時區,可以是EST,CST,MST,PST等,如果有var屬性,則將結果存儲在所設定的範圍之內.在屬性範圍內的頁面都會使用該時區爲默認時區.
<fmt:setLocale value="zh_cn" />
<fmt:setTimeZone value="EST" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:setLocale value="zh_cn" />
<fmt:setTimeZone value="EST" />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" /><br />
輸出:
上午09時25分12秒 EST
上午09時25分12秒 EST
上午09時25分12秒 EST
此時區在該頁面內都有效
九:<fmt:timeZone>
-------------------------------------------------------------------
用來暫時設置時區.
<fmt:setLocale value="zh_cn" />
<fmt:timeZone value="EST">
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
</fmt:timeZone>
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<fmt:setLocale value="zh_cn" />
<fmt:timeZone value="EST">
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
<br />
</fmt:timeZone>
<fmt:formatDate value="<%=new Date()%>" type="time" timeStyle="full" />
此標籤的時區只是部分,在標籤開始至標籤結束內有效,其它地方無效,其它地方還是會使用默認時區
<fn:> Function標籤 庫
==========================================================================
JSTL Functions 標籤庫中提供了一組常用的 EL 函數,主要用於處理字符串,在 JSP 中可以直接使用這些函數。
在 JSP 文件中使用 Functions 標籤庫,要先通過 taglib 指令引入該標籤庫:
<%@taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %.
18.1fn:contains 函數
fn:contains 函數用於判斷在源字符串中是否包含目標字符串,其語法爲:
fn:contains(String source,String target) -------boolean;
以上 source 參數指定源字符串, target 參數指定目標字符串,返回類型爲 boolean 。
例如對於以下 EL 表達式:
${fn:contains(“Tomcat”,”cat”)}
${fn:contains(“Tomcat”,”CAT”)}
第一個 EL 表達式的值爲 true ,第二個 EL 表達式的值爲 false 。
18.2fn:containsIgnoreCase 函數
fn:containsIgnoreCase 函數用於判斷在源字符串中是否包含目標字符串,並且在判斷時忽略大小寫,其語法爲:
fn: containsIgnoreCase (String source,String target) -------boolean;
以上 source 參數指定源字符串, target 參數指定目標字符串,返回類型爲 boolean 。
例如對於以下 EL 表達式:
${fn: containsIgnoreCase (“Tomcat”,”CAT”)}
${fn: containsIgnoreCase (“Tomcat”,”Mike”)}
第一個 EL 表達式的值爲 true ,第二個 EL 表達式的值爲 false 。
18.3 fn:startsWith 函數
fn:startsWith 函數用於判斷源字符串是否以指定的目標字符串開頭,其語法爲:
fn:startsWith(String source,String target) ----boolean
以上 source 參數指定源字符串, target 參數指定目標字符串,返回類型爲 boolean 。
例如對於以下 EL 表達式:
${fn: startsWith (“Tomcat”,”Tom”)}
${fn: startsWith (“Tomcat”,”cat”)}
第一個 EL 表達式的值爲 true ,第二個 EL 表達式的值爲 false 。
18.4 fn:endsWith 函數
fn: endsWith 函數用於判斷源字符串是否以指定的目標字符串結尾,其語法爲:
fn: endsWith (String source,String target) ----boolean
以上 source 參數指定源字符串, target 參數指定目標字符串,返回類型爲 boolean 。
例如對於以下 EL 表達式:
${fn: endsWith (“Tomcat”,”cat”)}
${fn: endsWith (“Tomcat”,”Tom”)}
第一個 EL 表達式的值爲 true ,第二個 EL 表達式的值爲 false 。
18.5 fn:indexOf 函數
fn:indexOf 函數用於在源字符串中查找目標字符串,並返回源字符串中最先與目標字符串匹配的第一個字符的索引,如果在源字符串中不包含目標字符串,就返回 -1 ,源字符串中的第一個字符的索引爲 0 。 fn:indexOf 函數的語法爲:
fn: indexOf (String source,String target) ----int
以上 source 參數指定源字符串, target 參數指定目標字符串,返回類型爲 int 。
例如對於以下 EL 表達式:
1 ${fn: indexOf (“Tomcat”,”cat”)}<br/>
2 ${fn: indexOf (“2211221”,”21”)} <br/>
3 ${fn: indexOf (“Tomcat”,”Mike”)} <br/>
其輸出結果爲:
1 3
2 1
3 -1
18.6 fn:replace 函數
fn:replace 函數用於把源字符串中的一部分替換爲另外的字符串,並返回替換後的字符串。 fn:replace 函數的語法爲:
fn: replace (String source,String before,String after) ----String
以上 source 參數指定源字符串, before 參數指定源字符串中被替換的子字符串, after 參數指定用於替換的子字符串,返回類型爲 String 。
例如對於以下 EL 表達式:
1 ${ fn: replace(“TomcAt”,”cAt”,”cat”)}<br/>
2 ${ fn: replace(“2008/1/9”,”/”,”-”)}<br/>
其輸出結果爲:
1 Tomcat
2 2008-1-9
18.7 fn:substring 函數
fn:substring 函數用於獲取源字符串中的特定子字符串,它的語法爲:
fn:substring(String source,int beginIndex,int endIndex) ------String
以上 source 參數指定源字符串, beginIndex 參數表示子字符串中的第一個字符在源字符串中的索引, endIndex 參數表示子字符串的最後一個字符在源字符串中的索引加 1 ,返回類型爲 String ,源字符串中的第一個字符的索引爲 0 。
例如對於以下 EL 表達式:
1 ${ fn: substring (“Tomcat”,0,3)}<br/>
2 ${ fn: substring (“Tomcat”,3,”6”)}<br/>
其輸出結果爲:
1 Tom
2 cat
18.8 fn:substringBefore 函數
fn:substringBefore 函數用於獲取源字符串中指定子字符串之前的子字符串,其語法爲:
fn:substringBefore(String source,String target) ----String
以上 source 參數指定源字符串, target 參數指定子字符串,返回類型爲 String 。如果在源字符串中不包含特定子字符串,就返回空字符串。
例如對於以下 EL 表達式:
1 ${ fn: substringBefore (“Tomcat”,”cat”)}<br/>
2 ${ fn: substringBefore (“mydata.txt”,”.txt”)}<br/>
其輸出結果爲:
1 Tom
2 mydata
18.9 fn:substringAfter 函數
fn: substringAfter 函數用於獲取源字符串中指定子字符串之後的子字符串,其語法爲:
fn: substringAfter (String source,String target) ----String
以上 source 參數指定源字符串, target 參數指定子字符串,返回類型爲 String 。如果在源字符串中不包含特定子字符串,就返回空字符串。
例如對於以下 EL 表達式:
1 ${ fn: substringAfter (“Tomcat”,”Tom”)}<br/>
2 ${ fn: substringAfter (“mydata.txt”,” mydata.”)}<br/>
其輸出結果爲:
1 cat
2 txt
18.10 fn:split 函數
fn:split 函數用於將源字符串拆分爲一個字符串數組,其語法爲:
fn: split (String source,String delimiter) ----String[]
以上 source 參數指定源字符串, delimiter 參數指定用於拆分源字符串的分隔符,返回類型爲 String[] 。如果在源字符串中不包含 delimiter 參數指定的分隔符,或者 delimiter 參數爲 null ,那麼在返回的字符串數組中只有一個元素,爲源字符串。
例如對於以下 EL 表達式:
<c:set value=’${ fn: split (“www.mywebsite.org”,”.”)}’ var=”strs”/>
<c:forEach var=”token” item=”${strs}”>
${token}<br/>
</c:forEach>
其輸出結果爲:
www
mywebsite
org
再例如對於以下代碼:
<c:set value=’${ fn: split (“www.mywebsite.org”,”-”)}’ var=”strs”/>
${strs[0]}
其輸出結果爲:
www.mywebsite.org
18.11 fn:join 函數
fn:join 函數用於將源字符串數組中的所有字符串連接爲一個字符串,其語法爲:
fn:join(String source[],String separator) ----String
以上 source 參數指定源字符串數組, separator 參數指定用於連接源字符串數組中的各個字符串的分隔符,返回類型爲 String 。
例如對於以下代碼:
<%
String strs[] = {“www”,”mywebsite”,”org”};
%>
<c:set value=”<%=strs%>” var=”strs”/>
${fn:join(strs,”.”)}
其輸出結果爲:
www. mywebsite. org
18.12 fn:toLowerCase 函數
fn:toLowerCase 函數用於將源字符串中的所有字符改爲小寫,其語法爲:
fn:toLowerCase(String source) -----String
以上 source 參數指定源字符串,返回類型爲 String 。
例如對於以下 EL 表達式:
fn:toLowerCase(“TomCat”)
其輸出結果爲:
tomcat
18.13 fn:toUpperCase 函數
fn: toUpperCase 函數用於將源字符串中的所有字符改爲大寫,其語法爲:
fn: toUpperCase (String source) -----String
以上 source 參數指定源字符串,返回類型爲 String 。
例如對於以下 EL 表達式:
fn: toUpperCase (“TomCat”)
其輸出結果爲:
TOMCAT
18.14 fn:trim 函數
fn:trim 函數用於將源字符串中的開頭和末尾的空格刪除,其語法爲:
fn:trim(String source) ----String
以上 source 參數指定源字符串,返回類型爲 String 。
例如對於以下 EL 表達式:
fn:trim(“ Tomcat ”)
以上 EL 表達式的值爲“ Tomcat ”。
18.15 fn:escapeXml 函數
fn:escapeXml 函數用於將源字符串中的字符“ < ”、“ > ”、“ ” ”和“ & ”等轉換爲轉義字符,本書第 1 章的 1.2 節( HTML 簡介)介紹了轉義字符的概念。 fn:escapeXml 函數的行爲與 <c:out> 標籤的 escapeXml 屬性爲 true 時的轉換行爲相同, fn:escapeXml 函數的語法爲:
fn:escapeXml(String source) ----String
以上 source 參數指定源字符串,返回類型爲 String 。
例程 18-1 的 out.jsp 演示了 fn:escapeXml 函數的用法。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>out</title> </head> <body> 1.${fn:escapeXml("<b> 表示粗體字 </b>") }<br/> 2.<c:out value="<b> 表示粗體字 </b>" escapeXml="true"></c:out><br/> 3.${"<b> 表示粗體字 </b>"}<br/> </body> </html> |
對於 out.jsp 中的以下代碼:
1.${fn:escapeXml("<b> 表示粗體字 </b>") }<br/>
2.<c:out value="<b> 表示粗體字 </b>" escapeXml="true"></c:out><br/>
3.${"<b> 表示粗體字 </b>"}<br/>
其輸出結果爲:
1.<b> 表示粗體字 </b><br/>
2.<b> 表示粗體字 </b><br/>
3.<b> 表示粗體字 </b><br/>
18.16 fn:length 函數
fn:length 函數用於返回字符串中的字符的個數,或者集合和數組的元素的個數,其語法爲:
fn:length(source) ---- int
以上 source 參數可以爲字符串、集合或者數組,返回類型爲 int 。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@page import="java.util.ArrayList"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>length</title> </head> <body> <% int[] array = {1,2,3,4}; ArrayList list = new ArrayList(); list.add("one"); list.add("two"); list.add("three"); %> <c:set value="<%=array%>" var="array"></c:set> <c:set value="<%=list%>" var="list"></c:set> 數組長度: ${fn:length(array)}<br/> 集合長度: ${fn:length(list)}<br/> 字符串長度: ${fn:length("Tomcat")}<br/> </body> </html> |
Functions 標籤庫概覽
l fn:contains 函數 : 用於判斷在源字符串中是否包含目標字符串。
l fn:containsIgnoreCase 函數 : 用於判斷在源字符串中是否包含目標字符串 , 並且在判斷時忽略大小寫。
l fn:startsWith 函數 : 用於判斷源字符串是否以指定的目標字符串開頭。
l fn: endsWith 函數:用於判斷源字符串是否以指定的目標字符串結尾。
l fn:indexOf 函數:用於在源字符串中查找目標字符串,並返回源字符串中最先與目標字符串匹配的第一個字符的索引。
l fn:replace 函數:用於把源字符串中的一部分替換爲另外的字符串,並返回替換後的字符串。
l fn:substring 函數:用於獲取源字符串中的特定子字符串。
l fn:substringBefore 函數:用於獲取源字符串中指定子字符串之前的子字符串。
l fn: substringAfter 函數:用於獲取源字符串中指定子字符串之後的子字符串
l fn:split 函數:用於將源字符串拆分爲一個字符串數組。
l fn:join 函數:用於將源字符串數組中的所有字符串連接爲一個字符串。
l fn:toLowerCase 函數:用於將源字符串中的所有字符改爲小寫。
l fn: toUpperCase 函數:用於將源字符串中的所有字符改爲大寫。
l fn:trim 函數:用於將源字符串中的開頭和末尾的空格刪除。
l fn:escapeXml 函數:用於將源字符串中的字符“ < ”、“ > ”、“ ” ”和“ & ”等轉換爲轉義字符。
l fn:length 函數:用於返回字符串中的字符的個數,或者集合和數組的元素的個數