CÔNG NGHỆ JAVA
ThS.Nguyễn Hữu Thể
JSP - Standard Tag Library (JSTL)
1
JavaServer Pages Standard Tag Library (JSTL)
− Thư viện thẻ chuẩn, cung cấp các thẻ để kiểm soát hành vi
trang, lặp đi lặp lại và các lệnh điều khiển, các thẻ quốc tế hóa, và các thẻ SQL.
− JSTL là một phần của Java EE API. − Để sử dụng JSTL => cần phải tải về các thư viện JSTL, đặt các
thư viện này vào thư mục WEB-INF/lib của project.
− Các nhóm thư viện thẻ JSTL:
1. Core Tags: Nhóm thẻ cơ bản 2. Formatting tags: Nhóm thẻ định dạng 3. SQL tags: Nhóm thẻ SQL 4. XML tags: Nhóm thẻ XML 5.
JSTL Functions: Nhóm hàm JSTL
2
Các thẻ cơ bản (Core Tags)
− Các thẻ cơ bản cung cấp hỗ trợ cho bộ lặp (iteration), các điều kiện logic, bắt ngoại lệ, url, chuyển tiếp (forward) hoặc chuyển hướng (redirect),...
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3
Các thẻ định dạng và địa phương hóa (Formatting and Localization Tags)
− Những thẻ này cung cấp định dạng cho các con số, ngày
tháng
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
4
Các thẻ SQL (SQL Tags)
− Các thẻ JSTL SQL cung cấp các hỗ trợ cho việc tương tác
với cơ sở dữ liệu quan hệ như Oracle, MySql...
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
5
Các thẻ XML (XML Tags)
− Thẻ XML được sử dụng để làm việc với các tài liệu XML như phân tích cú pháp XML, chuyển đổi dữ liệu XML và XPath đánh giá biểu thức.
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
6
Các thẻ hàm JSTL (JSTL Functions Tags)
− Thẻ JSTL cung cấp một số chức năng mà chúng ta có thể sử dụng để thực hiện các toán tử dùng chung, xử lý chuỗi
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
7
Download thư viện JSTL
Thư viện JSTL (Cung cấp bởi Apache)
Thư viện JSTL (Cung cấp bởi Glassfish)
taglibs-standard-spec-*.jar javax.servlet.jsp.jslt-api-*.jar
taglibs-standard-impl-*.jar javax.servlet.jsp.jstl-*.jar
8
Các thẻ JSTL cơ bản (JSTL Core Tags)
Thẻ
Miêu tả Giống <%= ... >, nhưng cho các Expression
Thiết lập kết quả của một ước lượng Expression trong một 'scope'
Gỡ bỏ một biến mục tiêu (từ một biến scope cụ thể, nếu đã xác định)
Bắt bất kỳ Throwable mà xuất hiện trong thân của nó và trưng bày nó một cách tùy ý
Thẻ điều kiện đơn giản, mà ước lượng phần thân của nó nếu điều kiện đã cho là true
Thẻ điều kiện đơn giản mà thiết lập một context cho các hoạt
động điều kiện loại trừ, được đánh dấu bởi
Thẻ phụ của
9
Các thẻ JSTL cơ bản (JSTL Core Tags)
Thẻ
Miêu tả
Thẻ phụ của mà theo sau thẻ và chỉ chạy
nếu tất cả điều kiện trước được ước lượng là 'false'
Thu nhận một URL tuyệt đối hoặc quan hệ và trưng bày nội dung của nó tới hoặc trang đó, một String trong 'var', hoặc một Reader trong 'varReader'
Thẻ lặp cơ bản, chấp nhận nhiều kiểu tập hợp khác nhau và hỗ trợ subsetting (chia tập con) và tính năng khác
Lặp qua các token, được phân biệt bởi các dấu phân tách (delimiter) đã cung cấp
Thêm một parameter tới một URL của thẻ đang chứa 'import'
Redirect tới một URL mới
Tạo một URL với các tham số truy vấn tùy ý
10
− The
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
11
− It is used to set the result of an expression evaluated in a
'scope’. Scope: có 4 phạm vi: 1. page 2. application 3. request 4. session
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Output:
12
16000
Attribute Description
Required Default
value Information to save No body
property
No
None
var
No
None
target No None
Name of the variable whose property should be modified Property to modify Name of the variable to store information
scope No Page Scope of variable to store information
13
− The
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
14
− The < c:if > tag is used for testing the condition and it display
the body content, if the expression evaluated is true. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
My income is:
− Output:
▪ My income is: 16000
15
Attribute Description Required Default
test Yes None
var No None Condition to evaluate Name of the variable to store the condition's result
scope No page Scope of the variable to store the condition's result
16
− It is used for removing the specified variable from a particular
scope.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Before Remove Value is:
After Remove Value is:
Output:
Before Remove Value is: 16000 After Remove Value is:
17
− It is used for Catches any Throwable exceptions that occurs in the body and optionally exposes it. In general it is used for error handling and to deal more easily with the problem occur in program. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% int x = 2/0;%>
The type of exception is : ${catchtheException}
There is an exception: ${catchtheException.message}
Output: The type of exception is : java.lang.ArithmaticException: / by zero 18 There is an exception: / by zero
, ,
− The < c:choose > tag is a conditional tag that establish a
context for mutually exclusive conditional operations. It works like a Java switch statement in which we choose between a numbers of alternatives.
− The
− The < c:otherwise > is also subtag of < choose > it follows
body if the condition evaluated be 'true'.
− The c:when and c:otherwise works like if-else statement. But it
must be placed inside c:choose tag.
, ,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Your income is :
Income is not good.
Income is very good.
Income is undetermined...
Output: 1.Your income is : 16000 2.Income is very good.
, ,
Even/Odd Example using c:when and c:otherwise <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL c:when, c:otherwise, c:choose
Output: 10 is even number
− The
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Item
Output:
Item 1 Item 2 Item 3
22
Attribute Description
Required Default
items Information to loop over None No
begin No 0 Element to start with (0 = first item, 1 = second item, ...)
step
Process every step items
No
1
end No Element to end with (0 = first item, 1 = second item, ...) Last element
var No None Name of the variable to expose the current item
varStatus No None Name of the variable to expose the loop status
23
< c:forTokens >
− The < c:forTokens > tag iterates over tokens which is
separated by the supplied delimeters. It is used for break a string into tokens and iterate through each of the tokens to generate output.
Attribute Description
Required Default
delims
Characters to use as delimiters
Yes
None
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %
Output:
24
Rahul Nakul Rajesh
< c:param >
− The < c:param > tag add the parameter in a containing 'import' tag's URL. It allow the proper URL request parameter to be specified within URL and it automatically perform any necessary URL encoding.
− Inside < c:param > tag, the value attribute indicates the
parameter value and name attribute indicates the parameter name. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
${completeURL}
25
< c:redirect >
− The < c:redirect > tag redirects the browser to a new URL. It supports the context-relative URLs, and the < c:param > tag. − It is used for redirecting the browser to an alternate URL by
using automatic URL rewriting.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
26
Output: http://javatpoint.com
< c:url >
− The < c:url > tag creates a URL with optional query parameter.
− The JSTL url tag is used as an alternative method of writing call to the response.encodeURL() method. The advantage of url tag is proper URL encoding and including the parameters specified by children. param tag.
It is used for url encoding or url formatting. This tag automatically performs the URL rewriting operation.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>