Professional Documents
Culture Documents
JSP Custom Tag123
JSP Custom Tag123
JSP Custom Tag123
Ni dung trnh by
Tag Library
Custom Tag
Simple Custom Tag Attribute Custom Tag
TAG LIBRARY
Cch thc Tag x l Tag Library Descriptor File M t Tag JSP Page Khai bo v s dng Tag
JSP Page
Import Tag Library Tham chiu n URL ca TLD Khai bo Tag Prefix S dng cc Tag
<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
11
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
out session
request response exception errorData page config context
13
Include Forward
14
15
16
17
18
19
20
21
22
Kt qu
23
Kt qu
24
<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
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
out session
request response exception errorData page config context
30
Include Forward
31
32
33
<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>
36
public class EmployeeTag extends TagSupport { private String employeeID; private String employeeName; public EmployeeTag() { this.employeeID = ""; this.employeeName = ""; } . . .
37
38
39
<?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>
<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 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>
<% 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"/>
44
45
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
Trong Tag Handler Class Thm thuc tnh attribute1, attribute2,,atrributeN Thm cc phng thc:
public void setAttribute1(String value1){}
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
out session
request response exception errorData page config context
50
Include Forward
51
52
53
<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>
JSP Content
</prefix>
56
57
58
59
<?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>
61
62
63
65
66
67
68
70
71
72
73
74
75
76
77
78
79
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