May 12, 2021 JSP
The JSP Standard Label Library (JSTL) is a collection of JSP labels that encapsulates the common core functionality of JSP applications.
JSTL supports common, structured tasks such as iterations, conditional judgment, XML document operations, internationalization tags, and SQL tags. In addition to these, it provides a framework to use custom labels that integrate JSTL.
Depending on the features provided by the JSTL label, it can be divided into five categories.
Apache Tomcat installs the JSTL library as follows:
Next, let'.xml the following configuration in the web server file:
……
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
</taglib>
</jsp-config>
……
To use any library, you must include the label at the head of each JSP file.
Core labels are the most commonly used JSTL labels. The syntax that refers to the core tag library is as follows:
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Label | Describe |
---|---|
<c:out> | Used to display data in JSP, just like . > |
<c:set> | Used to save data |
<c:remove> | Used to delete data |
<c:catch> | Used to handle exceptions that produce errors and store error messages |
<c:if> | Same as the if we use in general programs |
<c:choose> | Itself is only used as the parent label of the parent label of the sl;c:when;and the parent label of the "when"; and the "otherwise" |
<c:when> | Sub-labels of the select; used to determine whether conditions are valid |
<c:otherwise> | The sub-labels of the sl;c:choose> are followed by the labels, which are executed when the labels are judged to be false |
<c:import> | Retrieve an absolute or relative URL and expose its contents to the page |
<c:forEach> | The underlying iterative label accepts multiple collection types |
<c:forTokens> | Separate the content and iterate the output according to the specified separator |
<c:param> | Used to pass parameters to pages that contain or redirect |
<c:redirect> | Redirect to a new URL. |
<c:url> | Use optional query parameters to create a URL |
JSTL formatted labels are used to format and output text, date, time, and numbers. The syntax for referencing a formatted label library is as follows:
<%@ taglib prefix="fmt"
uri="http://java.sun.com/jsp/jstl/fmt" %>
Label | Describe |
---|---|
<fmt:formatNumber> | Format the numbers in the specified format or precision |
<fmt:parseNumber> | Resolve a string that represents a number, currency, or percentage |
<fmt:formatDate> | Format the date and time with the specified style or pattern |
<fmt:parseDate> | Resolve a string that represents a date or time |
<fmt:bundle> | The resource is bound |
<fmt:setLocale> | The specified region |
<fmt:setBundle> | The resource is bound |
<fmt:timeZone> | Specify the time zone |
<fmt:setTimeZone> | Specify the time zone |
<fmt:message> | Displays resource profile information |
<fmt:requestEncoding> | Set the character encoding of the request |
The JSTL SQL Tag Library provides tags that interact with relationship databases (Oracle, MySQL, SQL Server, and so on). The syntax that refers to the SQL tag library is as follows:
<%@ taglib prefix="sql"
uri="http://java.sun.com/jsp/jstl/sql" %>
Label | Describe |
---|---|
<sql:setDataSource> | Specify the data source |
<sql:query> | Run the SQL query statement |
<sql:update> | Run the SQL update statement |
<sql:param> | Set the parameters in the SQL statement to the specified value |
<sql:dateParam> | Set the date parameter in the SQL statement to the specified java.util.Date object value |
<sql:transaction> | Provides nested database behavior elements in a shared database connection, running all statements as a transaction |
The JSTL XML Tag Library provides labels for creating and operating XML documents. The syntax for referencing the XML tag library is as follows:
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
Before you can use the xml tag, you must copy the relevant packages for XML and XPath to your installation directory under .
Download address: http://www.apache.org/dist/xerces/j/
Download address: http://xml.apache.org/xalan-j/index.html
Label | Describe |
---|---|
<x:out> | And . . Similar, but only for XPath expressions |
<x:parse> | Resolve XML data |
<x:set> | Set the XPath expression |
<x:if> | To judge the XPath expression, if true, execute the contents of the ont body, otherwise skip the ont body |
<x:forEach> | Iterates the nodes in the XML document |
<x:choose> | Parent labels for slt;x:when> and .lt;x:otherwise> |
<x:when> | Sub-labels for conditional judgment |
<x:otherwise> | The sub-labels of the slt;x:choose> are executed when they are judged to be false |
<x:transform> | Apply XSL transformations to XML documents |
<x:param> | Used in use with the slt;x:transform;to set up XSL style sheets |
JSTL contains a series of standard functions, most of which are generic string handling functions. The syntax that refers to the JSTL library is as follows:
<%@ taglib prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions" %>
Function | Describe |
---|---|
fn:contains() | Test whether the string you enter contains the specified subchain |
fn:containsIgnoreCase() | Test whether the string you enter contains the specified substring, and the case is insensitive |
fn:endsWith() | Test whether the entered string ends with the specified suffix |
fn:escapeXml() | Skip characters that can be tagged as XML |
fn:indexOf() | Returns the location where the specified string appears in the input string |
fn:join() | The elements in the array are synthesized into a string and then output |
fn:length() | Returns the length of the string |
fn:replace() | Replace the location specified in the input string with the specified string and return it |
fn:split() | Separate the strings with the specified separators and form an array of substrings and return them |
fn:startsWith() | Test whether the input string starts with the specified prefix |
fn:substring() | Returns a subset of the string |
fn:substringAfter() | Returns a subset of the string after the specified substruce |
fn:substringBefore() | Returns a subset of the string before the specified substruce |
fn:toLowerCase() | Turn the characters in the string into small case |
fn:toUpperCase() | Turn the characters in the string into capitals |
fn:trim() | Remove the first blank character |