CÔNG NGHỆ THÔNG TIN >> BÀI VIẾT CHỌN LỌC

[Java Web] Tìm hiểu ngôn ngữ JSTL trong JSP Servlet

Đăng lúc: 02:12 PM - 01/02/2024 bởi Charles Chung - 676

Trong bài viết này chúng ta sẽ tìm hiểu về JSP Standard Tag Library(JSTL) trong JSP Servlet

Giới thiệu

JSTL hỗ trợ tới các nhiệm vụ phổ biến và có tính cấu trúc, ví dụ như các vòng lặp và câu lệnh điều kiện, các thẻ để thao tác tài liệu XML, các thẻ đa ngôn ngữ, và các thẻ SQL. Nó cũng cung cấp Framework để tích hợp các Custom Tags với các thẻ JSTL.

Các thẻ JSTL có thể được phân loại, theo tính năng của nó, sau đây là các nhóm thẻ JSTL có thể được sử dụng trong trang JSP:

  • Core Tags: Nhóm thẻ cơ bản

  • Formatting tags: Nhóm thẻ định dạng

  • SQL tags: Nhóm thẻ SQL

  • XML tags: Nhóm thẻ XML

  • JSTL Functions: Nhóm hàm JSTL

Cài đặt thư viện JSTL

Có 2 cách để cài đặt thư viện JSTL vào project của bạn

  1. Tải thư viện JSTL tại đây và copy các file JAR vào thư mục webapps\ROOT\WEB-INF\lib trong ứng dụng của bạn.
  2. Khai báo dependency maven trong file pom.xml của project
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Để sử dụng bất kỳ thư viện nào, bạn phải bao một <taglib> tại trên cùng của mỗi JSP mà sử dụng thư viện đó.

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

Bảng dưới liệt kê các thẻ trong nhóm Core Tags trong JSTL:

S.No. Tag & Description
1 <c:out>

Like <%= ... >, but for expressions.

2 <c:set >

Sets the result of an expression evaluation in a 'scope'

3 <c:remove >

Removes a scoped variable (from a particular scope, if specified).

4 <c:catch>

Catches any Throwable that occurs in its body and optionally exposes it.

5 <c:if>

Simple conditional tag which evalutes its body if the supplied condition is true.

6 <c:choose>

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>.

7 <c:when>

Subtag of <choose> that includes its body if its condition evalutes to 'true'.

8 <c:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated to 'false'.

9 <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'.

10 <c:forEach >

The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .

11 <c:forTokens>

Iterates over tokens, separated by the supplied delimeters.

12 <c:param>

Adds a parameter to a containing 'import' tag's URL.

13 <c:redirect >

Redirects to a new URL.

14 <c:url>

Creates a URL with optional query parameters

Nhóm Formatting Tags trong JSTL

Nhóm Formatting Tags trong JSTL được sử dụng để định dạng và hiển thị text, date, time và số ngôn ngữ trong Website. Sau đây là cú pháp cơ bản để include thư viện thẻ định dạng trong JSP của bạn.

<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>

Bảng dưới liệt kê các thẻ trong nhóm Formatting Tags trong JSTL:

S.No. Tag & Description
1 <fmt:formatNumber>

To render numerical value with specific precision or format.

2 <fmt:parseNumber>

Parses the string representation of a number, currency, or percentage.

3 <fmt:formatDate>

Formats a date and/or time using the supplied styles and pattern.

4 <fmt:parseDate>

Parses the string representation of a date and/or time

5 <fmt:bundle>

Loads a resource bundle to be used by its tag body.

6 <fmt:setLocale>

Stores the given locale in the locale configuration variable.

7 <fmt:setBundle>

Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.

8 <fmt:timeZone>

Specifies the time zone for any time formatting or parsing actions nested in its body.

9 <fmt:setTimeZone>

Stores the given time zone in the time zone configuration variable

10 <fmt:message>

Displays an internationalized message.

11 <fmt:requestEncoding>

Sets the request character encoding

Nhóm SQL Tags trong JSTL

Nhóm SQL Tags trong JSTL cung cấp các thẻ để tương tác với các Relational Database (RDBMSs), ví dụ như Oracle, MySQL, hoặc Microsoft SQL Server.

Đây là cú pháp để bao nhóm SQL Tags trong JSTL trong JSP của bạn:

<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>

Bảng dưới liệt kê các thẻ trong nhóm SQL Tags trong JSP:

S.No. Tag & Description
1 <sql:setDataSource>

Creates a simple DataSource suitable only for prototyping

2 <sql:query>

Executes the SQL query defined in its body or through the sql attribute.

3 <sql:update>

Executes the SQL update defined in its body or through the sql attribute.

4 <sql:param>

Sets a parameter in an SQL statement to the specified value.

5 <sql:dateParam>

Sets a parameter in an SQL statement to the specified java.util.Date value.

6 <sql:transaction >

Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

Nhóm XML Tags trong JSTL

Nhóm XML Tags trong JSTL cung cấp một cách để tạo và thao tác các tài liệu XML. Sau đây là cú pháp để include nhóm XML Tags trong JSP của bạn:

Thư viện thẻ XML Tags trong JSTL có các custom tags để tương tác với XML data. Điều này bao gồm việc parse XML, truyền tải XML data, và điều khiển dòng dữ liệu dựa trên các XPath Expression.

<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>

Dưới đây liệt kê các thẻ trong nhóm XML Tags trong JSTL:

S.No. Tag & Description
1 <x:out>

Like <%= ... >, but for XPath expressions.

2 <x:parse>

Used to parse the XML data specified either via an attribute or in the tag body.

3 <x:set >

Sets a variable to the value of an XPath expression.

4 <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.

5 <x:forEach>

To loop over nodes in an XML document.

6 <x:choose>

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> tags.

7 <x:when >

Subtag of <choose> that includes its body if its expression evalutes to 'true'.

8 <x:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluates to 'false'.

9 <x:transform >

Applies an XSL transformation on a XML document

10 <x:param >

Used along with the transform tag to set a parameter in the XSLT stylesheet

Nhóm JSTL Functions

JSTL bao gồm một số hàm chuẩn, mà hầu hết là các hàm thao tác chuỗi phổ biến. Sau đây là cú pháp để bao nhóm JSTL Functions trong chương trình JSP của bạn:

<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>

Bảng dưới liệt kê danh sách các JSTL Functions:

S.No. Function & Description
1 fn:contains()

Tests if an input string contains the specified substring.

2 fn:containsIgnoreCase()

Tests if an input string contains the specified substring in a case insensitive way.

3 fn:endsWith()

Tests if an input string ends with the specified suffix.

4 fn:escapeXml()

Escapes characters that can be interpreted as XML markup.

5 fn:indexOf()

Returns the index withing a string of the first occurrence of a specified substring.

6 fn:join()

Joins all elements of an array into a string.

7 fn:length()

Returns the number of items in a collection, or the number of characters in a string.

8 fn:replace()

Returns a string resulting from replacing in an input string all occurrences with a given string.

9 fn:split()

Splits a string into an array of substrings.

10 fn:startsWith()

Tests if an input string starts with the specified prefix.

11 fn:substring()

Returns a subset of a string.

12 fn:substringAfter()

Returns a subset of a string following a specific substring.

13 fn:substringBefore()

Returns a subset of a string before a specific substring.

14 fn:toLowerCase()

Converts all of the characters of a string to lower case.

15 fn:toUpperCase()

Converts all of the characters of a string to upper case.

16 fn:trim()

Removes white spaces from both ends of a string.

Nguồn tham khảo https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm, các bạn có thể bấm vào từng thẻ để xem cú pháp và cách dùng

Các ví dụ xem tại bài viết sau....

 

 

thay lời cảm ơn!

QUẢNG CÁO - TIẾP THỊ