session7 custom tags

Upload: duc-filan

Post on 04-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Session7 Custom Tags

    1/19

    JAD Session 14 - Custom Tags 1/14

    Session 14 - Custom Tags

    (chapter 8+9)

  • 8/13/2019 Session7 Custom Tags

    2/19

    JAD Session 14 - Custom Tags 2/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

  • 8/13/2019 Session7 Custom Tags

    3/19

    JAD Session 14 - Custom Tags 3/14

    Tag Extension Mechanism

    Enables creation of custom tags Java code can be avoided using custom tags

    The different terminologies related to tagextension mechanism are: Tag library

    TLD Custom action Custom

  • 8/13/2019 Session7 Custom Tags

    4/19

    JAD Session 14 - Custom Tags 4/14

    Tag extension mechanism..

    Tag library is acollection of customactions and tags

    Custom tagencapsulates complexrecurring code.

    Custom tag invokes acustom action

  • 8/13/2019 Session7 Custom Tags

    5/19

    JAD Session 14 - Custom Tags 5/14

    Custom Tags

    Separates the work profiles of Web designersand developers

    Custom tags can be reused Written using XML syntax The different types of custom tags are:

    Empty tag Tag with attributes Body tags

  • 8/13/2019 Session7 Custom Tags

    6/19

    JAD Session 14 - Custom Tags 6/14

    Custom Tags

    The function of the tag can be customizedusing the attributes in the custom tags

    TLD file contains the details of the tagattributes

    Custom tag body can include static text,HTML, and JSP elements like scriptlets,between the start and the end tag.

  • 8/13/2019 Session7 Custom Tags

    7/19

    JAD Session 14 - Custom Tags 7/14

    The Custom Tag Development Process

    There are four essential steps to writing acustom 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.

  • 8/13/2019 Session7 Custom Tags

    8/19

    JAD Session 14 - Custom Tags 8/14

    Hunting the Tag

    JSP Document

    Web Deployment Descriptor

    test_taglib

    /WEB-INF/myown-taglib.tld

    Tag Library Descriptor

    showDateexamples.ShowDateTagEMPTY

    Tag Handler Class1000001000000010101010000010101010000001001010000111111110000000001000001000000010101010

    00001010101000000100101000011111111000000000

    START HERE: tag used in JSP page.

    taglib uri declaration inJSP page maps to in deployment descriptor.

    indeployment descriptor maps to

    the real tag library descriptor file.

    Finally: in the TLD maps toa tag handler class.

  • 8/13/2019 Session7 Custom Tags

    9/19

    JAD Session 14 - Custom Tags 9/14

    Custom Tag Example

    Demo\CusTags

  • 8/13/2019 Session7 Custom Tags

    10/19

    JAD Session 14 - Custom Tags 10/14

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

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

  • 8/13/2019 Session7 Custom Tags

    11/19

    JAD Session 14 - Custom Tags 11/14

    The Classic Custom Tag Event Model

    JSP container processing one occurrence of a tag implementing the TagInterface in one JSP page.

  • 8/13/2019 Session7 Custom Tags

    12/19

  • 8/13/2019 Session7 Custom Tags

    13/19

    JAD Session 14 - Custom Tags 13/14

    Tags and Implicit Variables

  • 8/13/2019 Session7 Custom Tags

    14/19

    JAD Session 14 - Custom Tags 14/14

    Taglib step by step cc thnhphn c bn

  • 8/13/2019 Session7 Custom Tags

    15/19

    JAD Session 14 - Custom Tags 15/14

    Taglib step by step -Custom tagn gin

    T o file TLD (Tag Library Descriptor) Khi t o file TLD ch ng ta cn h nh dung tag c a ch ng ta c bao nhiuproperty.

    Trong v d ny ch ng ta s c 2property l tn ng i cn ch o m ngv ki u th hi n ng y th ng nm

    bd welcome taglib.customWelcome empty

    ten

    kieuNgay

  • 8/13/2019 Session7 Custom Tags

    16/19

    JAD Session 14 - Custom Tags 16/14

    Taglib step by step -Custom tagn gin

    S du ng Tag Library Descriptor: s d ng file TLD ch ng ta d ng 1 trong 2 c ch sau:- Khai b o tr c tip thng qua th nh phn directive taglig c atrang JSP:-

    Ho c khai b o trong web.xml :< jsp-config > < taglib >

    < taglib-uri >http://bodua.com < taglib-location > /WEB-

    INF/tlds/mySimpleTag.tld

  • 8/13/2019 Session7 Custom Tags

    17/19

    JAD Session 14 - Custom Tags 17/14

    Taglib step by step -Custom tagwith body

    Khi ch ng ta t o custom tag c n i dung, java class taghandler x ly tag cn c th c hi n kh c nhau t y thu c v on i dung c a th cn c thi h nh m t ln hay nhi u ln:Thi ha nh m t ln : nu n i dung ch cn thi h nh m t ln th tag

    handler nn implement interface Tag ho c extends abstractclass TagSupport. H m doStartTag c a tag handler cn returngi tr EVAL_BODY_INCLUDE trong tr ng h p ch ng ta cnthi h nh n i dung c a th hay return gi tri BODY_SKIP trongtr ng h p ng c l i.Thi ha nh nhi u ln : nu n i dung c a th cn thi h nh nhi uln, ch ng ta nn implements BodyTag interface l m tinterface extends t interface Tag c thm m t s h m h tr(setBodyContent, doInitBody va doAfterBody ) cho phep taghandler ki m tra v thay i n i dung th

  • 8/13/2019 Session7 Custom Tags

    18/19

    JAD Session 14 - Custom Tags 18/14

    Taglib step by step -Custom tagwith body

    Ngo i ra tng t nh TagSupport class, b n c th ch cnvit tag handler extend t BodyTagSupport implement tinterface BodyTag . V c b n ch cn vit code choh m doInitBody v doAfterBody .

    H m doInitBody c g i ra thi h nh sau khi n i dung th c c nhng tr c khi n i dung th thi h nhH m doAfterBody c g i ra sau khi n i dung th thi h nh.

  • 8/13/2019 Session7 Custom Tags

    19/19

    JAD Session 14 - Custom Tags 19/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