JSP Custom Tag123

You might also like

You are on page 1of 82

CHUYN JAVA

JSP CUSTOM TAG

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn

Ni dung trnh by
Tag Library

Custom Tag
Simple Custom Tag Attribute Custom Tag

Body Custom Tag


Nested Custom Tag

TAG LIBRARY

Cc thnh phn lin quan n Tag Library


Tag Handler Class

Cch thc Tag x l Tag Library Descriptor File M t Tag JSP Page Khai bo v s dng Tag

Tag Handler Class


Tag Handler chu trch nhim x l trn Tag, chuyn Tag thnh m ngun Java Tag Handler phi ci t li javax.servlet.jsp.tagext.Tag Thng k tha t TagSupport hoc BodyTagSupport Tag Handler trong Source Packages ca ng dng web ging nh Servlets, Java Beans

Tag Library Descriptor File


File XML m t Tag Name Cc Attribute Ch nh Tag Handler Class Th vin c khai bo v s dng cc Tag trong trang JSP

JSP Page
Import Tag Library Tham chiu n URL ca TLD Khai bo Tag Prefix S dng cc Tag

SIMPLE CUSTOM TAG

Simple Custom Tag


c im

Khng c thuc tnh


Khng c thn nh dng

<prefix:TagName/>

Cc bc xy dng v s dng
Bc 1: To lp Tag Handler Class k tha TagSupport trong Source Packages Bc 2: Tao Tag Library Descriptor thng tin v Simple Custom Tag C th m t thng tin nhiu Tag Bc 3: Khai bo v s dng Simple Custom Tag trong JSP (TLD) m t

10

B1:To Tag Handler Class k tha TagSupport


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

11

Bc 1: To lp Tag Handler Class


Trong Tag Handler Class

Ci t li doStartTag()
X l khi bt u m Tag Return SKIP_BODY Ci t li doEndTag() X l sau khi kt thc thn Tag Return EVAL_PAGE Trong doStartTag v doEndTag c th s dng

thuc tnh pageContext k tha c t TagSupport


12

B1:To Tag Handler Class k tha TagSupport


Mt s phng thc hay dng vi pageContext
i tng Phng thc

out session
request response exception errorData page config context

JspWriter getOut() HttpSession getSession()


ServletRequest getRequest() ServletResponse getResponse() Exception getException() ErrorData getErrorData() Object getPage() ServletConfig getServletConfig() ServletContext getServletContext()

13

B1:To Tag Handler Class k tha TagSupport


Include v Forward vi pageContext
Thao tc Phng thc

Include Forward

void include(String relativeURLPath) void forward(String relativeURLPath)

14

B2: To Tag Library Descriptor (TLD)


File XML m t Tag Name Cc Attribute Ch nh Tag Handler Class To TLD trong Web Pages

15

B2: To Tag Library Descriptor (TLD)


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

16

B2: To Tag Library Descriptor (TLD)


<body-content>empty</body-Content> : Khng c thn

17

B3: Khai bo v s dng trong JSP


Khai bo <%@taglib prefix="prefixName" uri="uri" %> S dng <prefixName:TagName/>

18

B1:To Tag Handler Class k tha TagSupport


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 cho Nguyn Hong 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; } }

19

B2: To Tag Library Descriptor (TLD)


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

20

B3: Khai bo v s dng trong JSP


Khai bo <%@taglib prefix="nhanh" uri="/WEB-INF/tlds/CustomTagLibrary" %> S dng <nhanh:HelloTag/>

21

B3: Khai bo v s dng trong JSP


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>

22

Kt qu

23

Kt qu

24

CUSTOM TAG WITH ATTRIBUTES


25

Custom Tag c thuc tnh


c im

C mt hay nhiu thuc tnh


Khng c thn nh dng

<prefix:TagName attribute1=
attrribute2=

attributeN=
/>
26

Cc bc xy dng v s dng
Bc 1: To lp Tag Handler Class k tha TagSupport trong Source Packages Bc 2: Tao Tag Library Descriptor thng tin v Simple Custom Tag C th m t thng tin nhiu Tag Bc 3: Khai bo v s dng Custom Tag c thuc tnh trong JSP (TLD) m t

27

B1:To Tag Handler Class k tha TagSupport


to c Custom Tag c thuc tnh
<prefix:TagName attribute1= attribute2= attributeN=/>

Trong Tag Handler Class Thm thuc tnh attribute1, attribute2,,atrributeN

Thm cc phng thc:


public void setAttribute1(String value1){} public void setAttribute2(String value2){} public void setAttributeN(String valueN){}

Ci t li phng thc doStartTag, doEndTag


28

Bc 1: To lp Tag Handler Class


Trong Tag Handler Class

Ci t li doStartTag()
X l khi bt u m Tag Return SKIP_BODY Ci t li doEndTag() X l sau khi kt thc thn Tag Return EVAL_PAGE Trong doStartTag v doEndTag c th s dng

thuc tnh pageContext k tha c t TagSupport


29

B1:To Tag Handler Class k tha TagSupport


Mt s phng thc hay dng vi pageContext
i tng Phng thc

out session
request response exception errorData page config context

JspWriter getOut() HttpSession getSession()


ServletRequest getRequest() ServletResponse getResponse() Exception getException() ErrorData getErrorData() Object getPage() ServletConfig getServletConfig() ServletContext getServletContext()

30

B1:To Tag Handler Class k tha TagSupport


Include v Forward vi pageContext
Thao tc Phng thc

Include Forward

void include(String relativeURLPath) void forward(String relativeURLPath)

31

B1:To Tag Handler Class k tha TagSupport


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

32

B1:To Tag Handler Class k tha TagSupport


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 dng cc thuc tnh ... return SKIP_BODY; } @Override public int doEndTag() { return EVAL_PAGE; } @Override public void release(){ this.attribute1=... this.attribute2=... . . . this.attributeN=... //Release ... } }

33

B2: To Tag Library Descriptor (TLD)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
34

<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: To Tag Library Descriptor (TLD)


<required>true</required> : Thuc tnh bt buc s dng trong Tag <required>false</required> : Thuc tnh ty chn s dng trong Tag <rtexprvalue>true</rtexprvalue> : Thuc tnh c th gn gi tr bng JSP Expression <rtexprvalue>false</rtexprvalue> : Thuc tnh khng c gn gi tr bng JSP Expression

<body-content>empty</body-Content> : Khng c thn


35

B3: Khai bo v s dng trong JSP


Khai bo <%@taglib prefix="prefixName" uri="URI TLD" %> S dng <prefixName:TagName attribute1=... attribute2=... ... attributeN=.../>

36

B1:To Tag Handler Class k tha TagSupport


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package customs;
import import import import import java.io.IOException; java.util.logging.Level; java.util.logging.Logger; javax.servlet.jsp.JspWriter; javax.servlet.jsp.tagext.TagSupport;

public class EmployeeTag extends TagSupport { private String employeeID; private String employeeName; public EmployeeTag() { this.employeeID = ""; this.employeeName = ""; } . . .

37

B1:To Tag Handler Class k tha TagSupport


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

38

B1:To Tag Handler Class k tha TagSupport


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


}

39

B2: To Tag Library Descriptor (TLD)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
40

<?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: To Tag Library Descriptor (TLD)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
41

<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 bo v s dng trong JSP


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
42

<%@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 bo v s dng trong JSP


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
43

<% String []names={"Nguyn Vn Tng", "Trn Thu Tho", "Nguyn Tin Sn", "L nh Ngc", "V ng Sn", "L Thnh Nhn", "Nguyn Ha Bnh", "Nguyn Ngc Trung", "L Vn Hong"}; for(int i=0; i<names.length; i=""++){ %> <nhanh:EmployeeTag employeeID="<%=String.valueOf(i+1)%>" employeeName="<%=names[i]%>"/> <%}%> <nhanh:EmployeeTag employeeID="10" employeeName="Nguyn Thnh Long"/> <nhanh:EmployeeTag employeeID="11" employeeName="Nguyn Thnh Trung"/>

B3: Khai bo v s dng trong JSP

44

BODY CUSTOM TAG

45

Body Custom Tag


c im

C thuc tnh
C thn nh dng
<prefix:TagName > JSP Content </prefix> <prefix:TagName attribute1= attributeN=> JSP Content

</prefix>

46

Cc bc xy dng v s dng
Bc 1: To lp Tag Handler Class k tha TagSupport trong Source Packages Bc 2: Tao Tag Library Descriptor thng tin v Body Custom Tag C th m t thng tin nhiu Tag Bc 3: Khai bo v s dng Body Custom Tag trong JSP (TLD) m t

47

Bc 1: To lp Tag Handler Class


to c Custom Tag c thuc tnh
<prefix:TagName attribute1= attribute2= attributeN=> JSP Content </prefix:TagName>

Trong Tag Handler Class Thm thuc tnh attribute1, attribute2,,atrributeN Thm cc phng thc:
public void setAttribute1(String value1){}

public void setAttribute2(String value2){}, ...


public void setAttributeN(String valueN){}
48

Bc 1: To lp Tag Handler Class


Trong Tag Handler Class

Ci t li doStartTag()
X l khi bt u m Tag Thng return EVAL_BODY_INCLUDE thay cho SKIP_BODY

Ci t li doEndTag()
X l sau khi kt thc thn Tag Return EVAL_PAGE

49

Bc 1: To lp Tag Handler Class


Mt s phng thc hay dng vi pageContext
i tng Phng thc

out session
request response exception errorData page config context

JspWriter getOut() HttpSession getSession()


ServletRequest getRequest() ServletResponse getResponse() Exception getException() ErrorData getErrorData() Object getPage() ServletConfig getServletConfig() ServletContext getServletContext()

50

Bc 1: To lp Tag Handler Class


Include v Forward vi pageContext
Thao tc Phng thc

Include Forward

void include(String relativeURLPath) void forward(String relativeURLPath)

51

B1:To Tag Handler Class k tha TagSupport


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

52

B1:To Tag Handler Class k tha TagSupport


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 dng cc thuc tnh ... return EVAL_BODY_INCLUDE; } @Override public int doEndTag() { return EVAL_PAGE; } @Override public void release(){ this.attribute1=... this.attribute2=... . . . this.attributeN=... //Release ... } }

53

B2: To Tag Library Descriptor (TLD)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
54

<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: To Tag Library Descriptor (TLD)


<required>true</required> : Thuc tnh bt buc s dng trong Tag <required>false</required> : Thuc tnh ty chn s dng trong Tag <rtexprvalue>true</rtexprvalue> : Thuc tnh c th gn gi tr bng JSP Expression <rtexprvalue>false</rtexprvalue> : Thuc tnh khng c gn gi tr bng JSP Expression

<body-content>JSP</body-Content> : Ni dung thn ca th l JSP


55

B3: Khai bo v s dng trong JSP


Khai bo <%@taglib prefix="prefixName" uri="URI TLD" %> S dng
<prefix:TagName attribute1= attributeN=>

JSP Content
</prefix>

56

Bc 1: To lp Tag Handler Class


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package customs;
import import import import import java.io.IOException; java.util.logging.Level; java.util.logging.Logger; javax.servlet.jsp.JspWriter; javax.servlet.jsp.tagext.TagSupport;

/** * * @author NHAnh */ public class CopyrightTag extends TagSupport {

private String bgColor;


public void setBgColor(String bgColor) { this.bgColor = bgColor; }

57

Bc 1: To lp Tag Handler Class


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

58

Bc 1: To lp Tag Handler Class


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

59

B2: To Tag Library Descriptor (TLD)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
60

<?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 bo v s dng trong JSP


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 Nguyn Hong Anh </nhanh:CopyrightTag> </body> </html>

61

B3: Khai bo v s dng trong JSP

62

NESTED CUSTOM TAG

63

Bc 1: To lp Parent Tag Handler Class


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

Bc 1: To lp Child Tag Handler Class


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

65

B2: To Tag Library Descriptor (TLD)


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

66

B3: Khai bo v s dng trong JSP


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 Nguyn Hong Anh </nhanh:ChildTag> </nhanh:ParentTag> <nhanh:ParentTag condition="false"> <nhanh:ChildTag> Khng xut hin </nhanh:ChildTag> </nhanh:ParentTag> </body>

67

SIMPLE IF CUSTOM TAG

68

Bc 1: To lp If Tag Handler Class


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

Bc 1: To lp Then Tag Handler Class


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

70

Bc 1: To lp Else Tag Handler Class


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

71

Bc 2: To Tag Library Descriptor (TLD)


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>

72

Bc 3: Khai bo v s dng trong JSP


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 Nguyn Hong Anh </nhanh:then> <nhanh:else> False, Hi Ng B Nam Phng </nhanh:else> </nhanh:if> <br/> <nhanh:if condition="false"> <nhanh:then> True, Hi Nguyn Hong Anh </nhanh:then> <nhanh:else> False, Hi Ng B Nam Phng </nhanh:else> </nhanh:if> </body>

73

Bc 3: Khai bo v s dng trong JSP

74

SIMPLE FOR CUSTOM TAG

75

Bc 1: To lp For Tag Handler Class


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

76

Bc 2: To Tag Library Descriptor (TLD)


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>

77

Bc 3: Khai bo v s dng trong JSP


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 Nguyn Hong Anh </nhanh:then> <nhanh:else> Hi Ng B Nam Phng </nhanh:else> </nhanh:if> <br/> </nhanh:for> </body>

78

Bc 3: Khai bo v s dng trong JSP

79

TI LIU THAM KHO

80

Tham kho
Marty Hall, Larry Brown, Core Servlets and JavaServer Pages: Volume 1: Core Technologies, 2nd Edition (2003) http://courses.coreservlets.com/CourseMaterials/csajsp2.html (2010)

81

HI V P

82

You might also like