Anda di halaman 1dari 19

Session 14 -Custom Tags

(chapter 8+9)

JAD Session 14 - Custom Tags

1/14

Objectives
Tag Libraries The Custom Tag Development Process Hunting the Tag Custom Tag Example JSTL (read at home)- JSP Standard Tag Library Tag Interfaces and Classes in javax.servlet.jsp.tagext The Classic Custom Tag Event Model Tags and Implicit Variables

JAD Session 14 - Custom Tags

2/14

Tag Extension Mechanism


Enables creation of custom tags
Java code can be avoided using custom tags The different terminologies related to tag extension mechanism are:
Tag library TLD Custom action Custom
JAD Session 14 - Custom Tags 3/14

Tag extension mechanism..


Tag library is a collection of custom actions and tags Custom tag encapsulates complex recurring code. Custom tag invokes a custom action

JAD Session 14 - Custom Tags

4/14

Custom Tags
Separates the work profiles of Web designers and developers Custom tags can be reused Written using XML syntax The different types of custom tags are:
Empty tag Tag with attributes Body tags
JAD Session 14 - Custom Tags 5/14

Custom Tags
The function of the tag can be customized using the attributes in the custom tags TLD file contains the details of the tag attributes Custom tag body can include static text, HTML, and JSP elements like scriptlets, between the start and the end tag.

JAD Session 14 - Custom Tags

6/14

The Custom Tag Development Process


There are four essential steps to writing a custom tag for use in your JavaServer Pages:
Writing a Java class called a tag handler Defining the tag within a tag library definition (TLD) file. Providing details of where to find the TLD file in the deployment descriptor, web.xml Referencing the TLD file in your JSP page source and using the tags from it.
JAD Session 14 - Custom Tags 7/14

Hunting the Tag


taglib uri declaration in JSP page maps to <taglib-uri> in deployment descriptor.

JSP Document
<html> <body> <%@taglib uri="test_taglib" prefix="myTag"%> <myTag:showDate/> </body> </html>

Web Deployment Descriptor


<web-app> <taglib> <taglib-uri> test_taglib </taglib-uri> <taglib-location> /WEB-INF/myown-taglib.tld </taglib-location> </taglib> </web-app> <taglib-location> in deployment descriptor maps to the real tag library descriptor file. Tag Handler Class 1000001000000010101010 0000101010100000010010 1000011111111000000000 1000001000000010101010 0000101010100000010010 1000011111111000000000 8/14

START HERE: tag used in JSP page.


Tag Library Descriptor <taglib> <tag> <name>showDate</name> <tagclass>examples.ShowDateTag</tagclass> <bodycontent>EMPTY</bodycontent> </tag> </taglib> Finally: <tag-class> in the TLD maps to a tag handler class.

JAD Session 14 - Custom Tags

Custom Tag Example

Demo\CusTags

JAD Session 14 - Custom Tags

9/14

Tag Interfaces and Classes in javax.servlet.jsp.tagext

Tag Interfaces and Classes in javax.servlet.jsp.tagext

JAD Session 14 - Custom Tags

10/14

The Classic Custom Tag Event Model

JSP container processing one occurrence of a tag implementing the Tag Interface in one JSP page.
JAD Session 14 - Custom Tags 11/14

Tags and Implicit Variables


Using the PageContext API we can write tag handler code to access the JSP implicit variables and access web application attributes.

JAD Session 14 - Custom Tags

12/14

Tags and Implicit Variables

JAD Session 14 - Custom Tags

13/14

Taglib step by step cc thnh phn c bn

JAD Session 14 - Custom Tags

14/14

Taglib step by step -Custom tag n gin


Tao file TLD (Tag Library Descriptor) Khi tao file TLD chung ta cn hinh dung tag cua chung ta co bao nhiu property. Trong vi du nay chung ta se co 2property la tn ngi cn chao mng va kiu th hin ngay thang nm

<short-name>bd</short-name> <tag> <name>welcome</name> <tag-class>taglib.customWelcome</tag-class> <body-content>empty</body-content> <attribute> <name>ten</name> </attribute> <attribute> <name>kieuNgay</name> </attribute> </tag>
JAD Session 14 - Custom Tags

15/14

Taglib step by step -Custom tag n gin


S dung Tag Library Descriptor: s dung file TLD chung ta dung 1 trong 2 cach sau: Khai bao trc tip thng qua thanh phn directive taglig cua trang JSP: <%@ taglib uri="/WEB-INF/tlds/mySimpleTag.tld" prefix="bd" %>Hoc khai bao trong web.xml: <jsp-config> <taglib> <taglib-uri>http://bodua.com</taglib-uri> <taglib-location>/WEBINF/tlds/mySimpleTag.tld</taglib-location> </taglib> </jsp-config>

JAD Session 14 - Custom Tags

16/14

Taglib step by step -Custom tag with body


Khi chung ta tao custom tag co ni dung, java class tag handler x ly tag cn c thc hin khac nhau tuy thuc vao ni dung cua the cn c thi hanh mt ln hay nhiu ln: Thi hanh mt ln: nu ni dung chi cn thi hanh mt ln thi tag handler nn implement interface Tag hoc extends abstract class TagSupport. Ham doStartTag cua tag handler cn return gia tri EVAL_BODY_INCLUDE trong trng hp chung ta cn thi hanh ni dung cua the hay return gia tri BODY_SKIP trong trng hp ngc lai. Thi hanh nhiu ln: nu ni dung cua the cn thi hanh nhiu ln, chung ta nn implements BodyTag interface la mt interface extends t interface Tag co thm mt s ham h tr (setBodyContent, doInitBody va doAfterBody) cho phep tag handler kim tra va thay i ni dung the
JAD Session 14 - Custom Tags 17/14

Taglib step by step -Custom tag with body


Ngoai ra tng t nh TagSupport class, ban co th chi cn vit tag handler extend t BodyTagSupport implement t interface BodyTag. V c ban chi cn vit code cho ham doInitBody va doAfterBody. Ham doInitBody c goi ra thi hanh sau khi ni dung the c oc nhng trc khi ni dung the thi hanh Ham doAfterBody c goi ra sau khi ni dung the thi hanh.

JAD Session 14 - Custom Tags

18/14

Summary
Tag Libraries The Custom Tag Development Process Hunting the Tag Custom Tag Example JSTL (read at home) Tag Interfaces and Classes in javax.servlet.jsp.tagext The Classic Custom Tag Event Model Tags and Implicit Variables

Q&A
JAD Session 14 - Custom Tags 19/14