Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
1. Slide 1 of 29
Introduction to Custom Tags
Classic tag
2. Slide 2 of 29
Overview
Custom Tags
Various type of Custom Tags
Components of Tag Library
javax.servlet.jsp.tagext package
Common methods of Custom Tags
3. Slide 3 of 29
Custom Tags
Allow Java programmer to embed Java
codes in JSP documents
Provide a mechanism to reuse and
encapsulate complex recurring code or
tasks in JSP
Provide simplicity and reusability of Java
code
Structure of custom tags in JSP is similar to
XML tags
4. Slide 4 of 29
Various Types of Custom Tag
Empty tags
<td:welcome />
Tags with attributes
<td: welcome color=”blue”></td:welcome>
Tags with a body
<td: welcome>
<%=today_date%>
</td:welcome>
Nested tags
<td1:ifTag condition “<%=eval>“ >
<td2:valueTrue>The expression evaluates to true
5. Slide 5 of 29
Component of Tag Library
Tag Handler
Tag Library Descriptor [TLD]
Tag File
6. Slide 6 of 29
Mechanisms for building custom tags
Tag Handler
Tag files
7. Slide 7 of 29
Mechanisms for building custom tags
Tag Handler
– Classic Tag
– Simple Tag
9. Slide 9 of 29
javax.servlet.jsp.tagext package
– Classic Custom Tag
Classic tags is a JSP 1.2 technology. The
classic tag handler class can be created by
extending implementing .
• Tag
• IterationTag
• BodyTag
• TagSupport
• BodyTagSupport
10. Slide 10 of 29
tagext package
Tag - Interface
– Is the base tag handler interface that defines
the methods required by all tag handlers
• public int doStartTag() throws JspException
• public int doEndTag() throws JspException
• public void release()
IterationTag - Interface
– Used when a custom tag needs to repeatedly
re-evaluate its body
– Useful for looping through a collection of
Objects without using a scriptlet
• public int doAfterBody() throws JspException
11. Slide 11 of 29
tagext package
BodyTag - Interface
– Provide features for manipulating the body content
• public void doInitBody() throws JspException
• public void setBodyContent(BodyContent b)
BodyContent - Class
– Is a subclass of the JSPWriter class
– represents the body content of a tag
TagSupport - Class
– Implements the Tag and IterationTag Interfaces.
– Supports tag and a tag with body iterations
12. Slide 12 of 29
tagext package
BodyTagSupport - Class
– Sub class of TagSupport & implements BodyTag
– used to develop custom tags with body
13. Slide 13 of 29
Tag Library Descriptor [TLD]
Is an xml file that contains the tag library
description
Contains the list and description of all custom
tags in the library
Provides the JSP engine with meta-information
about the custom tag and about its
implementation
Components of the TLD classified into two
groups
<taglib> </taglib>
<tag> </tag>
14. Slide 14 of 29
Structure of TLD file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>first</shortname>
<info>Example of a tag</info>
<tag>
<name>welcome</name>
<tagclass>TestTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>An example</info>
</tag>
</taglib>
15. Slide 15 of 29
Execution of JSP with Custom Tags
Execution Cycle of a JSP file with Custom Tags
16. Slide 16 of 29
Deploying and Using Custom Tags
Create a Tag Library Descriptor (.tld) file in the tlds folder
inside WEB-INF
Create a Tag Handler class in proper package with tld file.
Include taglib directive in target JSP page
Use the custom tag
17. Slide 17 of 29
tagext package
Returns value of doStartTag()
– SKIP_BODY
– EVAL_BODY_INCLUDE
– EVAL_BODY_BUFFERED
Return value of doEndTag()
– SKIP_PAGE
– EVAL_PAGE
Return value of doAfterBody()
– SKIP_BODY
– EVAL_BODY_AGAIN
18. Slide 18 of 29
Tag Life Cycle
Instantiate Tag
Handler
doStartTag()
doEndTag()
return
value
setBodyContent()
doInitBody()
doAfterBody()
return
value
Evaluate
Body
return
value
Evaluate rest
of the Page
Return from
current page
EVAL_BODY_BUFFERED
EVAL_BODY_INCLUD
E
SKIP_BODY
SKIP_BODY
EVAL_BODY_AGAIN
SKIP_PAGE
EVAL_PAGE
19. Slide 19 of 29
Summary
Custom Tags
Various type of Custom Tags
Components of Tag Library
javax.servlet.jsp.tagext package
Common methods of Custom Tags
Tag life cycle
Tag Library Descriptor
Execution of JSP with Custom Tags
Deploying and Using Custom Tags