10. jsp custom tag

82
CHUYÊN ĐỀ JAVA Nguyễn Hoàng Anh [email protected] JSP CUSTOM TAG

Upload: nguyen-mao

Post on 26-Oct-2015

44 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: 10. JSP Custom Tag

CHUYÊN ĐỀ JAVA

Nguyễn Hoàng Anh – [email protected]

JSP CUSTOM TAG

Page 2: 10. JSP Custom Tag

Nội dung trình bày

Tag Library

Custom Tag

– Simple Custom Tag

– Attribute Custom Tag

– Body Custom Tag

– Nested Custom Tag

Page 3: 10. JSP Custom Tag

TAG LIBRARY

3

Page 4: 10. JSP Custom Tag

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

Page 5: 10. JSP Custom Tag

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

Page 6: 10. JSP Custom Tag

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

Page 7: 10. JSP Custom Tag

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

Page 8: 10. JSP Custom Tag

SIMPLE CUSTOM TAG

8

Page 9: 10. JSP Custom Tag

Simple Custom Tag

Đặc điểm

– Không có thuộc tính

– Không có thân

Định dạng

<prefix:TagName/>

9

Page 10: 10. JSP Custom Tag

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

Page 11: 10. JSP Custom Tag

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;

}

}

Page 12: 10. JSP Custom Tag

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

Page 13: 10. JSP Custom Tag

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()

Page 14: 10. JSP Custom Tag

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)

Page 15: 10. JSP Custom Tag

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

Page 16: 10. JSP Custom Tag

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>

Page 17: 10. JSP Custom Tag

B2: Tạo Tag Library Descriptor (TLD)

<body-content>empty</body-Content> : Không có

thân

17

Page 18: 10. JSP Custom Tag

B3: Khai báo và sử dụng trong JSP

Khai báo

<%@taglib

prefix="prefixName"

uri="uri" %>

Sử dụng

<prefixName:TagName/>

18

Page 19: 10. JSP Custom Tag

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;

}

}

Page 20: 10. JSP Custom Tag

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>

Page 21: 10. JSP Custom Tag

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

Page 22: 10. JSP Custom Tag

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>

Page 23: 10. JSP Custom Tag

Kết quả

23

Page 24: 10. JSP Custom Tag

Kết quả

24

Page 25: 10. JSP Custom Tag

CUSTOM TAG WITH ATTRIBUTES

25

Page 26: 10. JSP Custom Tag

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

Page 27: 10. JSP Custom Tag

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

Page 28: 10. JSP Custom Tag

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

Page 29: 10. JSP Custom Tag

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

Page 30: 10. JSP Custom Tag

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()

Page 31: 10. JSP Custom Tag

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)

Page 32: 10. JSP Custom Tag

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;}

...

}

Page 33: 10. JSP Custom Tag

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

}

}

Page 34: 10. JSP Custom Tag

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>

Page 35: 10. JSP Custom Tag

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

Page 36: 10. JSP Custom Tag

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

Page 37: 10. JSP Custom Tag

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 = "";

}

. . .

Page 38: 10. JSP Custom Tag

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;

}

Page 39: 10. JSP Custom Tag

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;

}

}

Page 40: 10. JSP Custom Tag

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>

Page 41: 10. JSP Custom Tag

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>

Page 42: 10. JSP Custom Tag

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>

Page 43: 10. JSP Custom Tag

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"/>

Page 44: 10. JSP Custom Tag

B3: Khai báo và sử dụng trong JSP

44

Page 45: 10. JSP Custom Tag

BODY CUSTOM TAG

45

Page 46: 10. JSP Custom Tag

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

Page 47: 10. JSP Custom Tag

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

Page 48: 10. JSP Custom Tag

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

Page 49: 10. JSP Custom Tag

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

Page 50: 10. JSP Custom Tag

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()

Page 51: 10. JSP Custom Tag

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)

Page 52: 10. JSP Custom Tag

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;}

...

}

Page 53: 10. JSP Custom Tag

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

}

}

Page 54: 10. JSP Custom Tag

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>

Page 55: 10. JSP Custom Tag

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

Page 56: 10. JSP Custom Tag

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

Page 57: 10. JSP Custom Tag

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;

}

Page 58: 10. JSP Custom Tag

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;

}

Page 59: 10. JSP Custom Tag

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;

}

}

Page 60: 10. JSP Custom Tag

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>

Page 61: 10. JSP Custom Tag

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>

Page 62: 10. JSP Custom Tag

B3: Khai báo và sử dụng trong JSP

62

Page 63: 10. JSP Custom Tag

NESTED CUSTOM TAG

63

Page 64: 10. JSP Custom Tag

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;

}

}

Page 65: 10. JSP Custom Tag

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;

}

}

Page 66: 10. JSP Custom Tag

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>

...

Page 67: 10. JSP Custom 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>

Page 68: 10. JSP Custom Tag

SIMPLE IF CUSTOM TAG

68

Page 69: 10. JSP Custom Tag

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);

}

}

Page 70: 10. JSP Custom Tag

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);

}

}

}

Page 71: 10. JSP Custom Tag

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);

}

}

}

Page 72: 10. JSP Custom Tag

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>

Page 73: 10. JSP Custom 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>

Page 74: 10. JSP Custom Tag

Bước 3: Khai báo và sử dụng trong JSP

74

Page 75: 10. JSP Custom Tag

SIMPLE FOR CUSTOM TAG

75

Page 76: 10. JSP Custom Tag

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);

}

}

}

Page 77: 10. JSP Custom Tag

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>

Page 78: 10. JSP Custom 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>

Page 79: 10. JSP Custom Tag

Bước 3: Khai báo và sử dụng trong JSP

79

Page 80: 10. JSP Custom Tag

TÀI LIỆU THAM KHẢO

80

Page 81: 10. JSP Custom Tag

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

Page 82: 10. JSP Custom Tag

HỎI VÀ ĐÁP

82