Jakarta Standard Tag Library API 教程 (Jakarta Standard Tag Library API Tutorial
Jakarta Standard Tag Library (JSTL) API Tutorial
introduction:
Jakarta Standard Tag Library (JSTL) is a label library widely used on the Java platform to simplify and strengthen the development of the Java Server Pages (JSP) page.This tutorial will introduce the basic concepts, usage and related examples of JSTL API.
Table of contents:
1. Introduction to JSTL
2. JSTL core label library
2.1. C: Foreach tag
2.2. C: IF tag
2.3. C: Choose tag
2.4. C: SET tag
3. JSTL formatting label library
3.1. FMT: Formatdate tag
3.2. FMT: Formatnumber tag
4. JSTL database label library
4.1. SQL: SETDATAARCE label
4.2. SQL: Update tag
4.3. SQL: Query tag
5. JSTL function tag library
5.1. FN: LENGTH tag
5.2. Fn: ToupperCase tag
5.3. FN: Substring tag
6. JSTL international label library
6.1. FMT: SetLocale label
6.2. FMT: Bundle label
6.3. FMT: Message label
1. JSTL profile:
Jakarta Standard Tag Library (JSTL) is a label library that supports JSP pages.It provides a set of reusable labels that reduce the need to write complex logic on the JSP page and improve the readability and maintenance of code.The JSTL label library is divided into core label library, format tag library, database label library, function label library and international label library.
2. JSTL core label library:
2.1. C: Foreach tag:
C: Foreach label is used to recycling elements that traversed into a collection, array or specified range.The example code is as follows:
<c:forEach var="item" items="${myList}">
${item}
</c:forEach>
2.2. C: IF tag:
C: IF tag is used to execute code blocks on the JSP page based on conditions.The example code is as follows:
<c:if test="${condition}">
<!-- do something -->
</c:if>
2.3. C: Choose label:
C: CHOOSE tags are used to perform different code blocks according to multiple conditions.The example code is as follows:
<c:choose>
<c:when test="${condition1}">
<!-- do something -->
</c:when>
<c:when test="${condition2}">
<!-- do something else -->
</c:when>
<c:otherwise>
<!-- do something by default -->
</c:otherwise>
</c:choose>
2.4. C: SET tags:
C: SET tag is used to set the value of variables.The example code is as follows:
<c:set var="userName" value="John Doe" />
3. JSTL formatting label library:
3.1. FMT: Formatdate Tags:
FMT: FormatDate tag is used to format the date into a specific string.The example code is as follows:
<fmt:formatDate value="${nowDate}" pattern="yyyy-MM-dd HH:mm:ss" />
3.2. FMT: Formatnumber Tags:
FMT: Formatnumber tag is used to formatting numbers into specific string representations.The example code is as follows:
<fmt:formatNumber value="${myNumber}" pattern="#,##0.00" />
4. JSTL database tag library:
4.1. SQL: SetdataSource Tags:
SQL: SetdataSource label is used to set the data source of the database connection.The example code is as follows:
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" username="root" password="password" />
4.2. SQL: Update tag:
SQL: UPDATE tags are used to perform SQL update operations, such as inserting, updating, deleting.The example code is as follows:
<sql:update dataSource="${db}">
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
</sql:update>
4.3. SQL: Query tags:
SQL: Query tags are used to perform SQL query operations and store the results in variables.The example code is as follows:
<sql:query dataSource="${db}" var="result">
SELECT * FROM table_name
</sql:query>
5. JSTL function tag library:
5.1. FN: LENGTH tag:
Fn: Length tag is used to calculate the length of the string or collection.The example code is as follows:
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<fn:length var="${myString}" />
5.2. FN: ToupperCase Tags:
Fn: ToupperCase tag is used to convert the string into uppercase.The example code is as follows:
<fn:toUpperCase var="${myString}" />
5.3. Fn: substring label:
FN: Substring tag is used to obtain the sub -string of the string.The example code is as follows:
<fn:substring var="${myString}" start="0" end="5" />
6. JSTL international label library:
6.1. FMT: SetLocale label:
FMT: SetLocale label to set the language environment of the page.The example code is as follows:
<fmt:setLocale value="zh_CN" />
6.2. FMT: Bundle Tags:
FMT: Bundle label is used to load resource package files in a specific language environment.The example code is as follows:
<fmt:bundle basename="resources.MyResources" var="myRes" />
6.3. FMT: Message label:
FMT: MESSAGE tags are used to obtain specific texts in resource packages and displayed on the page.The example code is as follows:
<fmt:message key="welcome.message" bundle="${myRes}" />
This is the tutorial content about Jakarta Standard Tag Library (JSTL) API.I hope that this tutorial can help you understand the basic concepts and usage of JSTL.Please use JSTL to improve the development efficiency and code quality of the JSP page in actual development according to your needs.