Chapter 6: JSP Tag Libraries and JSTL - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Chapter 6: JSP Tag Libraries and JSTL

Description:

c:url value='/example1/estore.jsp' var='localURL' c:param name='catid' ... c:set var='EXAMPLE' value='/example1'/ c:set var='SHOP_PAGE' value='/estore.jsp' ... – PowerPoint PPT presentation

Number of Views:242
Avg rating:3.0/5.0
Slides: 14
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6: JSP Tag Libraries and JSTL


1
Chapter 6 JSP Tag Libraries and JSTL
Reference Beginning JSP
2
Objectives
  • Role of JSP Tag Libraries
  • Anatomy of a Tag Library
  • JSP Standard Tag Library (JSTL)
  • JSTL Tags
  • Converting the Shopping Cart
  • Declarations (including constants), iteration
  • URL creation, control flow, method calls

3
Role of JSP Tag Libraries
  • Reference Page 180
  • JSP Tag Library
  • Collection of custom actions (tags) available for
    JSP pages
  • Provide additional functionality beyond standard
    actions and implicit objects
  • Portable between different JSP containers
  • In general, HTML-like syntax easier to handle
    than embedded Java scriptlets

4
Anatomy of a Tag Library
  • Reference page 182
  • JAR archive
  • Tags (custom actions)
  • May use Java code or JSP code (Chapter 11)
  • TLD (Tag Library Descriptor)
  • File in XML format
  • Describes the tags
  • E.g., declares the functions used in JSP pages
  • Web.xml Deployment Descriptor
  • Taglib map (TLD location URI)
  • JSP page
  • Tag directive lt_at_ taglib uri gt

5
JSP Standard Tag Library (JSTL)
  • Reference page 181
  • Most popular tag library
  • Latest version JSTL 1.1
  • Covers the following
  • set and remove scoped variables, display
    expressions and values, catch expressions
  • Conditional flow control tags, Iteration tags
  • URL tags, format numbers and dates tags
  • Access to relational database (chapter 23)

6
JSTL Tags
  • Reference page 184-186, Appendix C
  • Core Tags
  • ltcifgt, ltcforEachgt, ltcchoosegt, ltcwhengt,
    ltcotherwisegt
  • ltcsetgt, ltcremovegt
  • ltcoutgt, ltcurlgt, ltccatchgt (Chapter 10)
  • Formatting Tags
  • ltfmtformatNumbergt, ltfmtformatDategt
  • XML and SQL Tags
  • ltcsqlgt (Chapter 23)

7
Converting the Shopping Cart
  • WARNING The ch06.war file does not have the Java
    sources
  • IMPORTANT When creating a Web project using
    existing sources in NetBeans 4.1, do not delete
    the class files.

8
Eliminate scripting declarations
  • Replace Java code in jspInit()
  • In new estore.jsp
  • ltcif test"empty cats"gt
  • ltcset var"cats" value"wxshopgetCats()"
    scope"application"/gt
  • lt/cifgt
  • Scoped variables
  • Objects that are attached as named attributes to
    the scoping implicit objects.
  • Scripting variables
  • Actual Java source code variables

9
Convert static method calls
  • E.g., EShop.getCats() in jspInit()
  • In web.xml
  • lttaglibgt
  • lttaglib-urigthttp//www.wrox.com/begjsp/eshop-
    functions-taglib
  • lt/taglib-urigt
  • lttaglib-locationgt/WEB-INF/jsp/eshop-taglib.tl
    d
  • lt/taglib-locationgt
  • lt/taglibgt
  • Add getCats() method in the TLD
  • ltfunctiongt
  • ltdescriptiongtObtain the catalog
    categorieslt/descriptiongt
  • ltnamegtgetCatslt/namegt
  • ltfunction-classgtcom.wrox.begjsp.ch03.EShoplt/funct
    ion-classgt
  • ltfunction-signaturegtjava.util.ArrayList
    getCats()lt/function-signaturegt
  • lt/functiongt

10
Use JSTL iteration tag
  • In new estore.jsp
  • ltcforEach var"curCat" items"cats"gt
  • ltcurl value"/example1/estore.jsp"
    var"localURL"gt
  • ltcparam name"catid" value"curCat.id"/gt
  • lt/curlgt
  • lta href"localURL" class"category"gtcurCat.n
    amelt/agt
  • lt/brgt
  • lt/cforEachgt
  • URL Creation Tag
  • ltcurlgt performs several tasks
  • Appends required context path
  • Encode URL parameters, etc.

11
Declaring constants, Remove variable
  • In new shopcart.jsp
  • ltcset var"EXAMPLE" value"/example1"/gt
  • ltcset var"SHOP_PAGE" value"/estore.jsp"/gt
  • ltcset var"CART_PAGE" value"/shopcart.jsp"/gt
  • Remove variable for reuse
  • ltcremove var"tmpitem"/gt
  • ltjspuseBean id"tmpitem" class"com.wrox.beg
    jsp.ch03.LineItem"gt
  • ltjspsetProperty name"tmpitem"
    property"quantity" value"1"/gt
  • lt/jspuseBeangt

12
Format Number, Convert method call
  • Format Number
  • ltfmtformatNumber value"curItem.price / 100"
    type"currency"/gt
  • No need to use dispPrice()
  • Convert method call
  • wxshopaddList(lineitems, tmpitem)
  • Add ltfunctiongt tag in the TLD file
  • ltfunctiongt
  • ltdescriptiongtAdd an item to a
    listlt/descriptiongt
  • ltnamegtaddListlt/namegt
  • ltfunction-classgtcom.wrox.begjsp.ch03.EShoplt/funct
    ion-classgt
  • ltfunction-signaturegtvoid addList(java.util.List,
    java.lang.Object)lt/function-signaturegt
  • lt/functiongt

13
Next Steps
  • Try It Out sections of Textbook (Chapter 6)
  • Example 1
  • Exercise 2 (Chapter 6)
  • Exercise 1 (optional)
Write a Comment
User Comments (0)
About PowerShow.com