Bài giảng Công nghệ Java - Bài 4, Phần 2: JSP. Standard Tag Library - Nguyễn Hữu Thể

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 JSTL3

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),.

pdf 27 trang kimcuc 5180
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Công nghệ Java - Bài 4, Phần 2: JSP. Standard Tag Library - Nguyễn Hữu Thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

Tóm tắt nội dung tài liệu: Bài giảng Công nghệ Java - Bài 4, Phần 2: JSP. Standard Tag Library - Nguyễn Hữu Thể

Bài giảng Công nghệ Java - Bài 4, Phần 2: JSP. Standard Tag Library - Nguyễn Hữu Thể
1CÔNG NGHỆ JAVA
JSP - Standard Tag 
Library (JSTL)
ThS.Nguyễn Hữu Thể
2JavaServer 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
3Cá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),...
4Cá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
5Cá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...
6Cá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. 
7Cá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
8Download 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
9Cá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 và 
 Thẻ phụ của mà include phần thân của nó nếu điều 
kiện được ước lượng là true
10
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 ý
11
− The tag is similar to JSP expression tag, but it can 
only be used with expression. It will display the result of an 
expression, similar to the way work.
12
− 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
Output:
16000
13
Attribute Description Required Default
value Information to save No body
target
Name of the variable whose 
property should be modified
No None
property Property to modify No None
var
Name of the variable to store 
information
No None
scope
Scope of variable to store 
information
No Page
14
− The is similar to jsp 'include', with an additional 
feature of including the content of any resource either within 
server or outside the server.
15
− The tag is used for testing the condition and it display 
the body content, if the expression evaluated is true.
− Output:
▪ My income is: 16000
 8000}">
My income is: 
16
Attribute Description Required Default
test Condition to evaluate Yes None
var
Name of the variable to store the 
condition's result
No None
scope
Scope of the variable to store the 
condition's result
No page
17
− It is used for removing the specified variable from a particular 
scope. 
Before Remove Value is: 
After Remove Value is: 
Output:
Before Remove Value is: 16000
After Remove Value is:
18
− 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.
The type of exception is : ${catchtheException} 
There is an exception: ${catchtheException.message}
Output:
The type of exception is : java.lang.ArithmaticException: / by zero
There is an exception: / by zero
, , 
− The 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 is subtag of that will include its 
body if the condition evaluated be 'true'.
− The is also subtag of it follows 
 tags and runs only if all the prior condition evaluated 
is 'false'.
− The c:when and c:otherwise works like if-else statement. But it 
must be placed inside c:choose tag.
, , 
Your income is : 
Income is not good.
 10000}">
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
JSTL c:when, c:otherwise, c:choose
 Output: 
10 is even number
22
− The is an iteration tag used for repeating the 
nested body content for fixed number of times or over the 
collection.
Item 
Output:
Item 1
Item 2
Item 3
23
Attribute Description Required Default
items Information to loop over No None
begin
Element to start with (0 = first 
item, 1 = second item, ...)
No 0
end
Element to end with (0 = first 
item, 1 = second item, ...)
No
Last 
element
step Process every step items No 1
var
Name of the variable to 
expose the current item
No None
varStatus
Name of the variable to 
expose the loop status
No None
24
− The 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.
<%@ taglib uri="" prefix="c" %
<c:forTokens items="Rahul-Nakul-Rajesh" delims="-
" var="name">
Output:
Rahul
Nakul
Rajesh
Attribute Description Required Default
delims Characters to use as delimiters Yes None
25
− The 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 tag, the value attribute indicates the 
parameter value and name attribute indicates the parameter 
name.
${completeURL}
26
− The tag redirects the browser to a new URL. It 
supports the context-relative URLs, and the tag.
− It is used for redirecting the browser to an alternate URL by 
using automatic URL rewriting.
1}">
Output:
27
− The tag creates a URL with optional query parameter. 
It is used for url encoding or url formatting. This tag 
automatically performs the URL rewriting operation.
− 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.

File đính kèm:

  • pdfbai_giang_cong_nghe_java_bai_4_phan_2_jsp_standard_tag_libra.pdf