17

Click here to load reader

® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

Embed Size (px)

DESCRIPTION

3 Overview of Tag Files  Another way to build custom actions  Tag files are JSPs with.tag or.tagx extensions  Allows non-Java developers to create reusable custom actions  Are automatically interpreted by the JSP container when placed in the /WEB-INF/tags folder  Custom action name is the same as the tag file name  Can be further organized in sub-folders of /WEB-INF/tags  Tag files are separate files that are called from JSPs Caller JSP …body content… TagFile2.tag TagFile1.tag

Citation preview

Page 1: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

®

IBM Software Group

© 2007 IBM Corporation

JSP Tag Files

4.1.0.3

Page 2: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

2

After completing this unit, you should be able to: Describe the use of JSP tag files Call a tag file from a JSP Pass body content, parameters and fragments to a tag file Create a tag file and use the tag file directives Package a tag file

Unit objectives

Page 3: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

3

Overview of Tag Files Another way to build custom actions

Tag files are JSPs with .tag or .tagx extensions Allows non-Java developers to create reusable custom actions Are automatically interpreted by the JSP container when

placed in the /WEB-INF/tags folderCustom action name is the same as the tag file nameCan be further organized in sub-folders of /WEB-INF/tags

Tag files are separate files that are called from JSPs

Caller JSP

<info:TagFile1 …/>

<info:TagFile2> …body content…</info:TagFile2>

TagFile2.tag

TagFile1.tag

Page 4: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

4

Calling a Tag File from a JSP Resembles the invocation of a custom action

Tag files are automatically compiled into SimpleTags More powerful than a <jsp:include> that can only accept string

parameters in the request The tag file has access to implicit objects The tag file can be passed

Body content: content in between the begin and end tagParametersFragments: individually named body content

For example to call details.tag from a caller JSP<%@taglib tagdir="/WEB-INF/tags" prefix="info" %>

<info:details loanedCopy="${item}"/>

Page 5: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

5

Body Content from Caller JSP Body Content can be: scriptless, tagdependent or empty Tag files can use <jsp:doBody/> to use the body content

Caller JSP<info:toUpper>Uppercase this text

</info:toUpper>

toUpper.tag (Tag file)<%@ tag body-content="scriptless"%><p style="text-transform:uppercase"><jsp:doBody/></p>

BodyContent

Page 6: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

6

Parameters from Caller JSP Passed as attributes to the tag file

Matches an attribute, with the same name, in the tag file<info:details loanedCopy="${item}"/>

Can be a dynamic list of attributesHandled in the tag file by a Map, thus order is not

guaranteedCan be mixed with defined attributes<info:details dyna1="first"loanedCopy="${item}"dyna2="second"/>

defined parameter mixed in with 2

dynamic parameters

parameter

Page 7: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

7

Fragments from Caller JSP Enable the calling page to pass in named fragments of body

content <jsp:body> is used to define the main body content

<info:fragmentTest><jsp:attribute name="headerFragment"><h1>Welcome to the IBM Library System </h1>

</jsp:attribute><jsp:attribute name="footerFragment">System has ${library.size} books

</jsp:attribute><jsp:body>

${item.title} ${item.author}</jsp:body>

</info:fragmentTest>

Page 8: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

8

Tag Directive in Tag File Attribute list for the directive

display-namebody-content: empty, scriptless or tagdependentdynamic-attributes: name of the attribute mapsmall-iconlarge-icondescriptionexamplelanguage: scripting languageimport: list of importspageEncodingisELIgnored: true of false

For example<%@ tag body-content="empty"%>

Page 9: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

9

Attribute Directive in Tag File Maps incoming parameters to variables Attribute list for the directive

namerequired: true or falsefragment: true or falsertexprvalue: true or falsetype: type of the attributedescription

For example<%@ attribute name="loanedCopy" required="true" type="com.ibm.library.model.LoanedCopy"%>

Dynamic attribute map, is defined in the tag directive<%@tag dynamic-attributes="elements" %><c:forEach var="element" items="${elements}" >

${element.key} : ${element.value}</c:forEach>

Page 10: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

10

Variable Directive in Tag File Exposes variables to the calling page A name can be given to the attribute or can be assigned from

an attributeif assigned from an attribute an alias needs to be defined to

reference the attribute within the tag file Attribute list for the directive

name-given or name-from attribute, with an aliasvariable-classscope: AT_BEGIN, AT_END or NESTEDdescription

For example <%@ variable name-given="returnValue"

scope="AT_BEGIN" %> <c:set var="returnValue" value="5% interest"/>

Page 11: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

11

Implicit Objects Available in a Tag File Collection of objects available to the tag file

request: ServletRequest or HttpServletRequestresponse: ServletResponse or HttpServletResponsejspContext: JspContext for this tag filesession: HttpSessionapplication: ServletContextout: JspWriterconfig: ServletConfig

Page 12: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

12

Example of Calling a Tag File Tag invocation in ListItems.jsp (Caller JSP)<%@taglib tagdir="/WEB-INF/tags" prefix="info" %>

<info:details loanedCopy="${item}"/>

Tag definition in details.tag (Tag file)<%@ tag body-content="empty"%><%@ attribute name="loanedCopy" required="true"

type="com.ibm.library.model.LoanedCopy"" %>

<h1>Times Renewed = ${loanedCopy.timesRenewed} </h1>

Page 13: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

13

Tag File with Body Content and a Fragment Tag files can be passed fragments and evaluate the

separately, using <jsp:invoke fragment="fragmentName"/>Calling JSP

<info:fragmentTest><jsp:attribute name="moreContent">Fragment 1</jsp:attribute><jsp:body>Body content</jsp:body>

</info:fragmentTest>fragmentTest.tag

<%@ attribute name="moreContent" fragment="true"%>

<jsp:invoke fragment="moreContent" /><jsp:doBody/>

Page 14: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

14

Tag File Packaging A tag file is recognized by the container if placed in /WEB-

INF/tags/ folderTag file does not need TLD

At tag file placed in the /META-INF/tags folder of a JAR file requires a TLDThe JAR file is placed in the /WEB-INF/lib folder

Tags can also be packaged as compiled Java classes

Page 15: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

15

Checkpoint1. Where can a tag file need to be placed to be automatically

picked up by the JSP container?2. Why use a tag file instead of writing a custom tag handler?

Page 16: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

16

Checkpoint solutions

1. /WEB-INF/tags2. Allows non-Java developers to create reusable custom

actions. Also externalizes any HTML and layout information that may creep into a tag handler class.

Page 17: ® IBM Software Group © 2007 IBM Corporation JSP Tag Files 4.1.0.3

17

Having completed this unit, you should be able to: Describe the advantages of using JSP custom tags List the major steps in developing and using JSP custom tags Develop basic tag handler classes to implement JSP custom

tags Create and modify taglib descriptor files Package JSP taglib implementation classes and taglib

descriptor files Understand the uses of the JSTL Name some of the tags included in the JSTL and their

purposes Create and call a tag file from a JSP

Unit summary