JSP標準標記庫(JSTL)教學
在JSP標準標記庫(JSTL)是很有用的JSP標簽的集合,它封裝常見的許多JSP應用程序的核心功能。
JSTL具有如迭代和條件,標簽為操縱XML文檔,國際化標簽,和SQL標簽常見,結構任務的支持。它還提供了集成現有的自定義標簽與JSTL標記框架。
JSTL標記可以進行分類,根據其功能,進入下麵可以創建一個JSP頁麵時,可以使用JSTL標簽庫組:
-
核心標簽
-
格式化標簽
-
SQL標記
-
XML標簽
-
JSTL功能
安裝JSTL庫:
如果您使用的是Apache Tomcat容器,那麼按照下麵兩個簡單的步驟:
-
從Apache標準標簽庫下載二進製分發和解壓的壓縮文件。
-
若要從Jakarta Taglibs中使用的標準標簽庫,隻需在分布的'lib'目錄複製JAR文件到您的應用程序的webappsROOTWEB-INFlib目錄中。
使用任何庫,必須包括<taglib>指令在每個JSP中使用該庫的頂部。
核心標簽:
標簽的核心小組是最常用的JSTL標記。以下是語法包括JSTL core庫在JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
有以下核心JSTL標簽:
標簽 | 描述 |
---|---|
<c:out > | Like <%= ... >, but for expressions. |
<c:set > | Sets the result of an expression evaluation in a 'scope' |
<c:remove > | Removes a scoped variable (from a particular scope, if specified). |
<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標簽的列表:
標簽 | 描述 |
---|---|
<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標記庫提供標簽為關係型數據庫(RDBMS),如Oracle,MySQL等,或Microsoft SQL Server進行交互。
以下是語法包括JSTL SQL庫在JSP:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
以下是SQL JSTL標簽的列表:
標簽 | 描述 |
---|---|
<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為中心的方式。以下是語法包括JSTL的XML庫在JSP。
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.htmll
以下是XML的JSTL標簽列表:
標簽 | 描述 |
---|---|
<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包含了一些標準函數,其中大部分是常用的字符串操作函數。以下是語法包括JSTL函數庫在JSP:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
下麵是JSTL函數的列表:
Function | 描述 |
---|---|
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. |