EBIZ 5535 Lecture 7 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

EBIZ 5535 Lecture 7

Description:

Custom tags are more powerful than beans. Custom tags require ... Accessing Custom Tags From JSP Files. Import the tag library. Specify location of TLD file ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 18
Provided by: jeffreyv
Category:
Tags: ebiz | custom | lecture

less

Transcript and Presenter's Notes

Title: EBIZ 5535 Lecture 7


1
EBIZ 5535Lecture 7
  • Custom JSP Tags

2
Custom JSP Tags
  • Permits JAVA developers to boil down complex
    server-side behavior into easy-to-use elements
    that content developers can use
  • Accomplish the same goals as beans
  • Encapsulating complex behaviors into a simple
    accessible form

3
Differences Between Beans and Custom Tags
  • Beans cannot manipulate JSP content custom tags
    can
  • Custom tags are more powerful than beans
  • Custom tags require more work to set up

4
Components of a Tag Library
  • The tag handler class
  • Java code that says how to actually translate tag
    into code
  • Must implement javax.servlet.jsp.tagext.Tag
    interface
  • Usually extends TagSupport or BodyTagSupport
  • Goes in the same directories as servlet class
    files and beans

5
Components Contd
  • The tag library descriptor file
  • XML file describing tag name, attributes and
    implementing tag handler class
  • Goes with JSP file or at arbitrary URL
  • The JSP file
  • Imports a tag library
  • Defines tag prefix
  • Uses the tags

6
Defining a Simple Tag Handler Class
  • Extend the TagSupport class
  • Imports needed packages
  • import javax.servlet.jsp.
  • import javax.servlet.jsp.tagext.
  • import java.io.
  • Override doStartTag
  • Obtain the JspWriter by means of
    pageContext.getOut()
  • Use the JspWriter

7
Simple Tag Handler Class Contd
  • Return SKIP_BODY
  • Translated into servlet code at page-translation
    time
  • Code is called at request time

8
Defining a Simple Tag Library Descriptor
  • Start with XML header and DOCTYPE
  • Top-level element is taglib
  • Each tag is defined by tag element with
  • name defines the base tag name
  • ltnamegtsimplePrimelt/namegt
  • tagclass gives the fully qualified class name of
    the tag handler
  • lttagclassgtprime.SimplePrimeTaglt/tagclassgt

9
Tag Library Descriptor Contd
  • bodycontent gives hints to development
    environments
  • info gives a short description
  • example-taglib.tld

10
Accessing Custom Tags From JSP Files
  • Import the tag library
  • Specify location of TLD file
  • lt_at_taglib urimytaglib.tld prefixmyprefix gt
  • Use the tags
  • ltmyprefixtagName /gt
  • Tag name comes from TLD file
  • Prefix comes from taglib directive

11
Simple Examples
  • SimpleExample.jsp
  • SimplePrimesExample.jsp

12
Using tags with attributes
  • ltprefixtagname
  • attrib1value1
  • attrib2value2 /gt
  • Still no tag body
  • Tag handling class needs getter and setter
    methods

13
Example of tag handler class with tag attributes
  • To support
  • ltprefixtagname attrib1value1/gt
  • Place the following method in the tag handler
    class for tagname
  • public void setAttrib1(String value)
  • doSomethingWith(value)
  • PrimeExample.jsp
  • How does the taglib change?

14
Including a tag body
  • Simple tags
  • ltprefixname/gt
  • Tags with attributes
  • ltprefixname attrib1value1 attrib2value2/gt
  • Now, tags with bodies
  • ltprefixname attrib3value3gt
  • JSP content
  • lt/prefixnamegt

15
Handler classes for tags with bodies
  • The doStartTag method
  • Instead of returning SKIP_BODY, return
    EVAL_BODY_INCLUDE
  • The doEndTag method
  • This method is only necessary if you want to take
    some action after handling the body
  • Return eval_page
  • HeadingExample.jsp
  • How does the taglib entry differ?

16
Optional tag bodies
  • First examples had no tag bodies
  • doStartTag returned SKIP_BODY
  • Then examples that include tag bodies
  • doStartTag returned EVAL_BODY_INCLUDE
  • We can decide whether or not to include the tag
    body at request time
  • Either return SKIP_BODY or EVAL_BODY_INCLUDE

17
Example of optionally including tagbody
  • DebugTag.java
Write a Comment
User Comments (0)
About PowerShow.com