8 Custom Tag

download 8 Custom Tag

of 32

Transcript of 8 Custom Tag

  • 7/27/2019 8 Custom Tag

    1/32

  • 7/27/2019 8 Custom Tag

    2/32

    JSP Custom Tags

    Custom Tags are User-defined tag library

    Provides the modularity

    Code reusability at Presentation Layer

    Makes JSP Development easier

    Provides clear separation between

    presentation design and code

  • 7/27/2019 8 Custom Tag

    3/32

    Basic Principles

    Every custom tag is associated with a Javaclass called as tag handler, kept inside apackage.

    The tag handler classes inherit the CustomTag API available injavax.servlet.jsp.tagext

    Collection of Custom Tags called as TagLibrary is configured using XML files (*.tldand web.xml)

  • 7/27/2019 8 Custom Tag

    4/32

    Usage of Tags

    Three kinds of usage of tags are

    Simple Tag

    Tag with Attribute

    Tag with Body Content

  • 7/27/2019 8 Custom Tag

    5/32

    Tag in Detail

    Start of Tag

    Tag

    Library

    The Tag

    End of Tag

    Attribute

    Value for

    Attribute

  • 7/27/2019 8 Custom Tag

    6/32

  • 7/27/2019 8 Custom Tag

    7/32

  • 7/27/2019 8 Custom Tag

    8/32

  • 7/27/2019 8 Custom Tag

    9/32

  • 7/27/2019 8 Custom Tag

    10/32

    Simple Tag Life Cycle

    Evaluate Body

    Done

    doTag()

    Load class

    SimpleTagHandler.class

    Instantiate the class

    No-arg constructor invokes

    Call

    setJspContext(JspContext)

    If the tag is nested,CallsetParent(JspTag)

    If the tag has attributes,

    call setters

    If the tag is not declared to have a

    of empty, And the tag has a body,call

    setJspBody(JspFragment)

    Call doTag()

    Container

    Performed

    tasks

  • 7/27/2019 8 Custom Tag

    11/32

  • 7/27/2019 8 Custom Tag

    12/32

    Life Cycle Methods (Simple)

    setPageContext() links the tag with current pagecontext

    setParent() Called only in the case of nested

    custom tags doStartTag() - Called when Start of Tag is

    encountered

    doEndTag() - Called when End of Tag is encountered

    release() - Called after End of Tag

  • 7/27/2019 8 Custom Tag

    13/32

    Page Context

    home.jsp

    user1

    user2

    user3

    pageContext for user1

    pageContext for user2

    pageContext for user3

    Custom

    Tag

    setPageContext()

  • 7/27/2019 8 Custom Tag

    14/32

  • 7/27/2019 8 Custom Tag

    15/32

  • 7/27/2019 8 Custom Tag

    16/32

  • 7/27/2019 8 Custom Tag

    17/32

    Making a Simple Tag handler

    Write the class SimpleTest that extends SimpleTagSupport

    Implementthe doTag() method

    Create a TLD for the tag customtag.tld

    Deploythe tag handler and TLD

    Write aJSP that uses the tag - customtag.jsp

  • 7/27/2019 8 Custom Tag

    18/32

    Tag Library Descriptors

    A tag library descriptor(TLD) is an XML

    document that describes a tag library.

    A TLD contains information about a library as a

    whole and about each tag contained in the

    library.

    TLDs are used by a Web container to validate

    the tags .

  • 7/27/2019 8 Custom Tag

    19/32

    Tag Element

    Each tag in the library is described by giving its

    name and the class of its tag handler, information

    on the scripting variables created by the tag, and

    information on the tag's attributes.

    Each attribute declaration contains an indication

    of whether the attribute is required, whether its

    value can be determined by request-timeexpressions, and the type of the attribute

  • 7/27/2019 8 Custom Tag

    20/32

    Classic Tag API

    JspTag

    //no methods, only for polymorphism and organization

    Tag

    int doEndTag()

    Tag getParent()

    int doStartTag()

    void setPageContext(PageContext)

    void setParent(Tag)

    void release()

  • 7/27/2019 8 Custom Tag

    21/32

    Classic tag handler Life Cycle-- TagSupport

    doStartTag()

    Evaluate Body

    doAfterBody()

    doEndTag()

    Done

    Evaluate Page

    return EVAL_BODY_INCLUDE

    return EVAL_BODY_AGAIN

    return SKIP_BODY

    return EVAL_PAGE

    return SKIP_PAGE

    return SKIP_BODY

  • 7/27/2019 8 Custom Tag

    22/32

    Load class -- SimpleTagHandler.class

    Instantiate the class -- no-arg constructor invokes

    Call setPageContext(PageContext)

    If the tag is nested,Call setParent(Tag)

    If the tag has attributes, call setters

    If the tag is not declared to have an empty body,

    And the tag is not invoked with an empty body,

    And the doStartTag() returns EVAL_BODY_INCLUDE,

    the body is evaluated. doInitBody() and

    setBodyContent() is called exactly only once

    doStartTag() returns EVAL_BODY_BUFFERED

    Call doEndTag()

    Call doStartTag()

    If the body content was evaluated , Call doAfterBody()

    Containers task

  • 7/27/2019 8 Custom Tag

    23/32

    Life Cycle Methods (Body Content)

    setPageContext()

    setParent()

    doStartTag()

    doInitBody()

    Called when body content begins

    doAfterBody() Called when body content ends

    doEndTag()

    release()

  • 7/27/2019 8 Custom Tag

    24/32

    Return Type constants

    Four important Constant identifiers are defined in

    Tag interface available through TagSupport class.

    These constants should be used as return type to

    control the flow of tag execution EVAL_BODY_INCLUDE

    SKIP_BODY

    EVAL_PAGE

    SKIP_PAGE

  • 7/27/2019 8 Custom Tag

    25/32

    Steps to Develop a Custom Tag

    A Tag Hanlder Class that extends either TagSupport

    or BodyTagSupport

    Override the lifecycle methods typically doStartTag()

    and doEndTag() A Tag Library Descriptor (TLD) file (under WEB-INF

    folder) that describes the tags in a tag library

    An entry in web.xml that refers the TLD file

    Using (invoking) the tag in JSP

  • 7/27/2019 8 Custom Tag

    26/32

  • 7/27/2019 8 Custom Tag

    27/32

    Programs

    MyTagHandler.java customtag1.tld

    customtag1.jsp

  • 7/27/2019 8 Custom Tag

    28/32

  • 7/27/2019 8 Custom Tag

    29/32

    Programs

    CubeNumber.java

    customtag2.tld

    customtag2.jsp

  • 7/27/2019 8 Custom Tag

    30/32

  • 7/27/2019 8 Custom Tag

    31/32

    Programs

    PowerNumber.java

    customtag3.tld

    customtag3.jsp

  • 7/27/2019 8 Custom Tag

    32/32

    Applications