10. jsp custom tag
TRANSCRIPT
Nội dung trình bày
Tag Library
Custom Tag
– Simple Custom Tag
– Attribute Custom Tag
– Body Custom Tag
– Nested Custom Tag
TAG LIBRARY
3
Các thành phần liên quan đến Tag Library
Tag Handler Class
– Cách thức Tag xử lý
Tag Library Descriptor File
– Mô tả Tag
JSP Page
– Khai báo và sử dụng Tag
4
Tag Handler Class
Tag Handler chịu trách nhiệm xử lý trên Tag, chuyển Tag
thành mã nguồn Java
Tag Handler phải cài đặt lại javax.servlet.jsp.tagext.Tag
Thường kế thừa từ TagSupport hoặc BodyTagSupport
Tag Handler để trong Source Packages của ứng dụng
web giống như Servlets, Java Beans
5
Tag Library Descriptor File
File XML mô tả
– Tag Name
– Các Attribute
– Chỉ định Tag Handler Class
Thư viện được khai báo và sử dụng các Tag trong trang JSP
6
JSP Page
Import Tag Library
– Tham chiếu đến URL của TLD
Khai báo Tag Prefix
Sử dụng các Tag
7
SIMPLE CUSTOM TAG
8
Simple Custom Tag
Đặc điểm
– Không có thuộc tính
– Không có thân
Định dạng
<prefix:TagName/>
9
Các bước xây dựng và sử dụng
Bước 1: Tạo lớp Tag Handler Class kế thừa
TagSupport trong Source Packages
Bước 2: Tao Tag Library Descriptor (TLD) mô tả
thông tin về Simple Custom Tag
– Có thể mô tả thông tin nhiều Tag
Bước 3: Khai báo và sử dụng Simple Custom Tag
trong JSP
10
B1:Tạo Tag Handler Class kế thừa TagSupport
11
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package packageName;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class TenTag extends TagSupport {
@Override
public int doStartTag() {
. . .
return SKIP_BODY;
}
@Override
public int doEndTag() {
return EVAL_PAGE;
}
}
Bước 1: Tạo lớp Tag Handler Class
Trong Tag Handler Class
– Cài đặt lại doStartTag()
• Xử lý khi bắt đầu mở Tag
• Return SKIP_BODY
– Cài đặt lại doEndTag()
• Xử lý sau khi kết thúc thân Tag
• Return EVAL_PAGE
Trong doStartTag và doEndTag có thể sử dụng
– thuộc tính pageContext kế thừa được từ TagSupport
12
B1:Tạo Tag Handler Class kế thừa TagSupport
Một số phương thức hay dùng với pageContext
13
Đối tượng Phương thức
out JspWriter getOut()
session HttpSession getSession()
request ServletRequest getRequest()
response ServletResponse getResponse()
exception Exception getException()
errorData ErrorData getErrorData()
page Object getPage()
config ServletConfig getServletConfig()
context ServletContext getServletContext()
B1:Tạo Tag Handler Class kế thừa TagSupport
Include và Forward với pageContext
14
Thao tác Phương thức
Include void include(String relativeURLPath)
Forward void forward(String relativeURLPath)
B2: Tạo Tag Library Descriptor (TLD)
File XML mô tả
– Tag Name
– Các Attribute
– Chỉ định Tag Handler Class
Tạo TLD trong Web Pages
15
B2: Tạo Tag Library Descriptor (TLD)
16
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-
jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>Short Name</short-name> <uri>URI</uri> <tag> <name>Tag Name</name> <tag-class>Tag Handler Class</tag-class> <body-content>empty</body-content> </tag> </taglib>
B2: Tạo Tag Library Descriptor (TLD)
<body-content>empty</body-Content> : Không có
thân
17
B3: Khai báo và sử dụng trong JSP
Khai báo
<%@taglib
prefix="prefixName"
uri="uri" %>
Sử dụng
<prefixName:TagName/>
18
B1:Tạo Tag Handler Class kế thừa TagSupport
19
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
public class HelloTag extends TagSupport {
@Override
public int doStartTag() {
JspWriter out = this.pageContext.getOut();
try {
out.println("<b>Xin chào Nguyễn Hoàng Anh</b>");
out.println("<b>This is a Simple Custom Tag</b>");
} catch (IOException ex) {
Logger.getLogger(HelloTag.class.getName())
.log(Level.SEVERE, null, ex);
}
return SKIP_BODY;
}
@Override
public int doEndTag() {
return EVAL_PAGE;
}
}
B2: Tạo Tag Library Descriptor (TLD)
20
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-
jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>customtaglibrary</short-name> <uri>/WEB-INF/tlds/CustomTagLibrary</uri> <tag> <name>HelloTag</name> <tag-class>customs.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
B3: Khai báo và sử dụng trong JSP
Khai báo
<%@taglib
prefix="nhanh"
uri="/WEB-INF/tlds/CustomTagLibrary" %>
Sử dụng
<nhanh:HelloTag/>
21
B3: Khai báo và sử dụng trong JSP
22
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="nhanh“
uri="/WEB-INF/tlds/CustomTagLibrary.tld" %>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<nhanh:HelloTag/>
</body>
</html>
Kết quả
23
Kết quả
24
CUSTOM TAG WITH ATTRIBUTES
25
Custom Tag có thuộc tính
Đặc điểm
– Có một hay nhiều thuộc tính
– Không có thân
Định dạng
<prefix:TagName attribute1=“…”
attrribute2=“…”
…
attributeN=“…”
/>
26
Các bước xây dựng và sử dụng
Bước 1: Tạo lớp Tag Handler Class kế thừa
TagSupport trong Source Packages
Bước 2: Tao Tag Library Descriptor (TLD) mô tả
thông tin về Simple Custom Tag
– Có thể mô tả thông tin nhiều Tag
Bước 3: Khai báo và sử dụng Custom Tag có thuộc
tính trong JSP
27
B1:Tạo Tag Handler Class kế thừa TagSupport
Để tạo được Custom Tag có thuộc tính
<prefix:TagName attribute1=“…” attribute2=“…”
… attributeN=“…”/>
Trong Tag Handler Class
– Thêm thuộc tính attribute1, attribute2,…,atrributeN
– Thêm các phương thức:
public void setAttribute1(String value1){…}
public void setAttribute2(String value2){…}
…
public void setAttributeN(String valueN){…}
– Cài đặt lại phương thức doStartTag, doEndTag
28
Bước 1: Tạo lớp Tag Handler Class
Trong Tag Handler Class
– Cài đặt lại doStartTag()
• Xử lý khi bắt đầu mở Tag
• Return SKIP_BODY
– Cài đặt lại doEndTag()
• Xử lý sau khi kết thúc thân Tag
• Return EVAL_PAGE
Trong doStartTag và doEndTag có thể sử dụng
– thuộc tính pageContext kế thừa được từ TagSupport
29
B1:Tạo Tag Handler Class kế thừa TagSupport
Một số phương thức hay dùng với pageContext
30
Đối tượng Phương thức
out JspWriter getOut()
session HttpSession getSession()
request ServletRequest getRequest()
response ServletResponse getResponse()
exception Exception getException()
errorData ErrorData getErrorData()
page Object getPage()
config ServletConfig getServletConfig()
context ServletContext getServletContext()
B1:Tạo Tag Handler Class kế thừa TagSupport
Include và Forward với pageContext
31
Thao tác Phương thức
Include void include(String relativeURLPath)
Forward void forward(String relativeURLPath)
B1:Tạo Tag Handler Class kế thừa TagSupport
32
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class TagName extends TagSupport {
private String attribute1;
private String attribute2;
...
private String attributeN;
public TagName(){
this.attribute1=...
this.attribute2=...
. . .
this.attributeN=...
}
public void setAttribute1(String value1){attribute1=value1;}
public void setAttribute2(String value2){attribute2=value2;}
...
public void setAttributeN(String valueN){attributeN=valueN;}
...
}
B1:Tạo Tag Handler Class kế thừa TagSupport
33
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class TagName extends TagSupport {
...
@Override
public int doStartTag() {
//Sử dụng các thuộc tính
...
return SKIP_BODY;
}
@Override
public int doEndTag() {
return EVAL_PAGE;
}
@Override
public void release(){
this.attribute1=...
this.attribute2=...
. . .
this.attributeN=...
//Release ...
}
}
B2: Tạo Tag Library Descriptor (TLD)
34
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
<taglib> <tlib-version>1.0</tlib-version>
<short-name>customtaglibrary</short-name><uri>URI TLD</uri>
<tag>
<name>Tag Name</name>
<tag-class>Tag Handler Class</tag-class>
<attribute>
<name>attribute1</name>
<required>true/false</required>
<rtexprvalue>true/false</rtexprvalue>
</attribute>
. . .
<attribute>
<name>attributeN</name>
<required>true/false</required>
<rtexprvalue>true/false</rtexprvalue>
</attribute>
<body-content>empty</body-content>
</tag>
</taglib>
B2: Tạo Tag Library Descriptor (TLD)
<required>true</required> : Thuộc tính bắt buộc sử
dụng trong Tag
<required>false</required> : Thuộc tính tùy chọn sử
dụng trong Tag
<rtexprvalue>true</rtexprvalue> : Thuộc tính có thể
gán giá trị bằng JSP Expression
<rtexprvalue>false</rtexprvalue> : Thuộc tính không
được gán giá trị bằng JSP Expression
<body-content>empty</body-Content> : Không có
thân
35
B3: Khai báo và sử dụng trong JSP
Khai báo
<%@taglib prefix="prefixName"
uri="URI TLD" %>
Sử dụng
<prefixName:TagName attribute1=“...”
attribute2=“...”
...
attributeN=“...”/>
36
B1:Tạo Tag Handler Class kế thừa TagSupport
37
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package customs;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class EmployeeTag extends TagSupport {
private String employeeID;
private String employeeName;
public EmployeeTag() {
this.employeeID = "";
this.employeeName = "";
}
. . .
B1:Tạo Tag Handler Class kế thừa TagSupport
38
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
B1:Tạo Tag Handler Class kế thừa TagSupport
39
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
@Override
public int doStartTag() {
JspWriter out = this.pageContext.getOut();
try {
out.println("<p>" + this.employeeID + " - "
+ this.employeeName + "</p>");
} catch (IOException ex) {
Logger.getLogger(EmployeeTag.class.getName()).
log(Level.SEVERE, null, ex);
}
return SKIP_BODY;
}
@Override
public void release() {
this.employeeID = null;
this.employeeName = null;
}
}
B2: Tạo Tag Library Descriptor (TLD)
40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>customtaglibrary</short-name>
<uri>/WEB-INF/tlds/CustomTagLibrary</uri>
<tag>
. . .
</tag>
</taglib>
B2: Tạo Tag Library Descriptor (TLD)
41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<tag>
<name>EmployeeTag</name>
<tag-class>customs.EmployeeTag</tag-class>
<attribute>
<name>employeeID</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>employeeName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>empty</body-content>
</tag>
</taglib>
B3: Khai báo và sử dụng trong JSP
42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="nhanh"
uri="/WEB-INF/tlds/CustomTagLibrary" %>
<html>
<head>
<meta http-equiv="Content-Type“
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
. . .
</body>
</html>
B3: Khai báo và sử dụng trong JSP
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%
String []names={"Nguyễn Văn Tùng", "Trần Thu Thảo",
"Nguyễn Tiến Sơn", "Lê Đình Ngọc",
"Võ Đông Sơn", "Lê Thành Nhân",
"Nguyễn Hòa Bình", "Nguyễn Ngọc Trung",
"Lê Văn Hoàng"};
for(int i=0; i<names.length; i=""++){
%>
<nhanh:EmployeeTag
employeeID="<%=String.valueOf(i+1)%>"
employeeName="<%=names[i]%>"/>
<%}%>
<nhanh:EmployeeTag employeeID="10"
employeeName="Nguyễn Thành Long"/>
<nhanh:EmployeeTag employeeID="11"
employeeName="Nguyễn Thành Trung"/>
B3: Khai báo và sử dụng trong JSP
44
BODY CUSTOM TAG
45
Body Custom Tag
Đặc điểm
– Có thuộc tính
– Có thân
Định dạng
<prefix:TagName >
JSP Content
</prefix>
<prefix:TagName attribute1=“…” … attributeN=“…”>
JSP Content
</prefix>
46
Các bước xây dựng và sử dụng
Bước 1: Tạo lớp Tag Handler Class kế thừa
TagSupport trong Source Packages
Bước 2: Tao Tag Library Descriptor (TLD) mô tả
thông tin về Body Custom Tag
– Có thể mô tả thông tin nhiều Tag
Bước 3: Khai báo và sử dụng Body Custom Tag trong
JSP
47
Bước 1: Tạo lớp Tag Handler Class
Để tạo được Custom Tag có thuộc tính
<prefix:TagName attribute1=“…” attribute2=“…”
attributeN=“…”>
JSP Content
</prefix:TagName>
Trong Tag Handler Class
– Thêm thuộc tính attribute1, attribute2,…,atrributeN
– Thêm các phương thức:
public void setAttribute1(String value1){…}
public void setAttribute2(String value2){…}, ...
public void setAttributeN(String valueN){…}
48
Bước 1: Tạo lớp Tag Handler Class
Trong Tag Handler Class
– Cài đặt lại doStartTag()
• Xử lý khi bắt đầu mở Tag
• Thường return EVAL_BODY_INCLUDE thay cho
SKIP_BODY
– Cài đặt lại doEndTag()
• Xử lý sau khi kết thúc thân Tag
• Return EVAL_PAGE
49
Bước 1: Tạo lớp Tag Handler Class
Một số phương thức hay dùng với pageContext
50
Đối tượng Phương thức
out JspWriter getOut()
session HttpSession getSession()
request ServletRequest getRequest()
response ServletResponse getResponse()
exception Exception getException()
errorData ErrorData getErrorData()
page Object getPage()
config ServletConfig getServletConfig()
context ServletContext getServletContext()
Bước 1: Tạo lớp Tag Handler Class
Include và Forward với pageContext
51
Thao tác Phương thức
Include void include(String relativeURLPath)
Forward void forward(String relativeURLPath)
B1:Tạo Tag Handler Class kế thừa TagSupport
52
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class TagName extends TagSupport {
private String attribute1;
private String attribute2;
...
private String attributeN;
public TagName(){
this.attribute1=...
this.attribute2=...
. . .
this.attributeN=...
}
public void setAttribute1(String value1){attribute1=value1;}
public void setAttribute2(String value2){attribute2=value2;}
...
public void setAttributeN(String valueN){attributeN=valueN;}
...
}
B1:Tạo Tag Handler Class kế thừa TagSupport
53
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class TagName extends TagSupport {
...
@Override
public int doStartTag() {
//Sử dụng các thuộc tính
...
return EVAL_BODY_INCLUDE;
}
@Override
public int doEndTag() {
return EVAL_PAGE;
}
@Override
public void release(){
this.attribute1=...
this.attribute2=...
. . .
this.attributeN=...
//Release ...
}
}
B2: Tạo Tag Library Descriptor (TLD)
54
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
<taglib> <tlib-version>1.0</tlib-version>
<short-name>customtaglibrary</short-name><uri>URI TLD</uri>
<tag>
<name>Tag Name</name>
<tag-class>Tag Handler Class</tag-class>
<attribute>
<name>attribute1</name>
<required>true/false</required>
<rtexprvalue>true/false</rtexprvalue>
</attribute>
. . .
<attribute>
<name>attributeN</name>
<required>true/false</required>
<rtexprvalue>true/false</rtexprvalue>
</attribute>
<body-content>JSP</body-content>
</tag>
</taglib>
B2: Tạo Tag Library Descriptor (TLD)
<required>true</required> : Thuộc tính bắt buộc sử
dụng trong Tag
<required>false</required> : Thuộc tính tùy chọn sử
dụng trong Tag
<rtexprvalue>true</rtexprvalue> : Thuộc tính có thể
gán giá trị bằng JSP Expression
<rtexprvalue>false</rtexprvalue> : Thuộc tính không
được gán giá trị bằng JSP Expression
<body-content>JSP</body-Content> : Nội dung thân
của thể là JSP
55
B3: Khai báo và sử dụng trong JSP
Khai báo
<%@taglib prefix="prefixName"
uri="URI TLD" %>
Sử dụng
<prefix:TagName attribute1=“…” … attributeN=“…”>
JSP Content
</prefix>
56
Bước 1: Tạo lớp Tag Handler Class
57
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package customs;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
*
* @author NHAnh
*/
public class CopyrightTag extends TagSupport {
private String bgColor;
public void setBgColor(String bgColor) {
this.bgColor = bgColor;
}
Bước 1: Tạo lớp Tag Handler Class
58
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
@Override
public int doStartTag() {
JspWriter out = this.pageContext.getOut();
try {
out.println("<table width='600' border='1'");
out.println("align='center' bgcolor='" +
this.bgColor + "' >");
out.println("<tr>");
out.println("<td>");
out.println("<center>");
} catch (IOException ex) {
Logger.getLogger(CopyrightTag.class.getName()).
log(Level.SEVERE, null, ex);
}
return EVAL_BODY_INCLUDE;
}
Bước 1: Tạo lớp Tag Handler Class
59
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
@Override
public int doEndTag() {
JspWriter out = this.pageContext.getOut();
try {
out.println("</center>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
} catch (IOException ex) {
Logger.getLogger(CopyrightTag.class.getName())
.log(Level.SEVERE, null, ex);
}
return EVAL_PAGE;
}
}
B2: Tạo Tag Library Descriptor (TLD)
60
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<short-name>customtaglibrary</short-name>
<uri>/WEB-INF/tlds/CustomTagLibrary</uri>
<tag>
<name>CopyrightTag</name>
<tag-class>customs.CopyrightTag</tag-class>
<attribute>
<name>bgColor</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>JSP</body-content>
</tag>
</taglib>
B3: Khai báo và sử dụng trong JSP
61
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="nhanh"
uri="/WEB-INF/tlds/CustomTagLibrary" %>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<nhanh:CopyrightTag bgColor="#0000FF">
Copyright @ 2010 by Nguyễn Hoàng Anh
</nhanh:CopyrightTag>
</body>
</html>
B3: Khai báo và sử dụng trong JSP
62
NESTED CUSTOM TAG
63
Bước 1: Tạo lớp Parent Tag Handler Class
64
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class ParentTag extends TagSupport{
private boolean condition;
public boolean getCondition() {
return condition;
}
public void setCondition(boolean condition) {
this.condition = condition;
}
@Override
public int doStartTag(){
return EVAL_BODY_INCLUDE;
}
@Override
public int doEndTag(){
return EVAL_PAGE;
}
}
Bước 1: Tạo lớp Child Tag Handler Class
65
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
public class ChildTag extends TagSupport {
@Override
public int doStartTag() {
ParentTag pt = (ParentTag) this.getParent();
if (pt.getCondition() == true) {
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}
@Override
public int doEndTag() {
return EVAL_PAGE;
}
}
B2: Tạo Tag Library Descriptor (TLD)
66
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
...
<tag>
<name>ParentTag</name>
<tag-class>customs.ParentTag</tag-class>
<attribute>
<name>condition</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>JSP</body-content>
</tag>
<tag>
<name>ChildTag</name>
<tag-class>customs.ChildTag</tag-class>
<body-content>JSP</body-content>
</tag>
...
B3: Khai báo và sử dụng trong JSP
67
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<body>
<nhanh:ParentTag condition="true">
<nhanh:ChildTag>
Hi Nguyễn Hoàng Anh
</nhanh:ChildTag>
</nhanh:ParentTag>
<nhanh:ParentTag condition="false">
<nhanh:ChildTag>
Không xuất hiện
</nhanh:ChildTag>
</nhanh:ParentTag>
</body>
SIMPLE IF CUSTOM TAG
68
Bước 1: Tạo lớp If Tag Handler Class
69
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package customs;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class IfTag extends SimpleTagSupport {
private boolean condition;
public boolean getCondition() {
return condition;
}
public void setCondition(boolean condition) {
this.condition = condition;
}
@Override
public void doTag() throws JspException, IOException{
this.getJspBody().invoke(null);
}
}
Bước 1: Tạo lớp Then Tag Handler Class
70
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package customs;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ThenTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
IfTag it=(IfTag)this.getParent();
if(it.getCondition()==true){
this.getJspBody().invoke(null);
}
}
}
Bước 1: Tạo lớp Else Tag Handler Class
71
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
package customs;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ElseTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
IfTag it = (IfTag) this.getParent();
if (it.getCondition() == false) {
this.getJspBody().invoke(null);
}
}
}
Bước 2: Tạo Tag Library Descriptor (TLD)
72
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<tag>
<name>if</name>
<tag-class>customs.IfTag</tag-class>
<attribute>
<name>condition</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>then</name>
<tag-class>customs.ThenTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>else</name>
<tag-class>customs.ElseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
Bước 3: Khai báo và sử dụng trong JSP
73
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<body>
<nhanh:if condition="true">
<nhanh:then>
True, Hi Nguyễn Hoàng Anh
</nhanh:then>
<nhanh:else>
False, Hi Ngô Bá Nam Phương
</nhanh:else>
</nhanh:if>
<br/>
<nhanh:if condition="false">
<nhanh:then>
True, Hi Nguyễn Hoàng Anh
</nhanh:then>
<nhanh:else>
False, Hi Ngô Bá Nam Phương
</nhanh:else>
</nhanh:if>
</body>
Bước 3: Khai báo và sử dụng trong JSP
74
SIMPLE FOR CUSTOM TAG
75
Bước 1: Tạo lớp For Tag Handler Class
76
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
package customs;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForTag extends SimpleTagSupport{
private int size;
public void setSize(int size) {
this.size = size;
}
@Override
public void doTag() throws JspException, IOException{
for(int i=0; i<this.size; i++){
this.getJspBody().invoke(null);
}
}
}
Bước 2: Tạo Tag Library Descriptor (TLD)
77
1 2 3 4 5 6 7 8 9
10 11
<tag>
<name>for</name>
<tag-class>customs.ForTag</tag-class>
<attribute>
<name>size</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<body-content>scriptless</body-content>
</tag>
Bước 3: Khai báo và sử dụng trong JSP
78
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20
<body>
<nhanh:for size="10">
<nhanh:if condition="true">
<nhanh:then>
Hi Nguyễn Hoàng Anh
</nhanh:then>
<nhanh:else>
Hi Ngô Bá Nam Phương
</nhanh:else>
</nhanh:if>
<br/>
</nhanh:for>
</body>
Bước 3: Khai báo và sử dụng trong JSP
79
TÀI LIỆU THAM KHẢO
80
Tham khảo
Marty Hall, Larry Brown, Core Servlets and
JavaServer Pages™: Volume 1: Core Technologies,
2nd Edition (2003)
http://courses.coreservlets.com/Course-
Materials/csajsp2.html (2010)
81
HỎI VÀ ĐÁP
82