SlideShare a Scribd company logo
JSP Custom Tags Svetlin Nakov National Academy for Software Development academy.devbg.org Creating and Using Custom Tags
Contents Tag Files Types of Tag Files JSTL in Tags Exporting Variables from Tags Modifying Body Content in Tags
Tag Files Tag files let you define your own custom JSP tags You should use tag files when: You want to modularize a part of a JSP’s output You want to perform some behavior in many places (reusable) This behavior can be presentation or application logic <books4u:shoppingCart currency=&quot;EUR&quot; />
Types of Tag Files Tag files come in two types: Stand-alone tags Tags containing other tags and text Both types of tags can have any number of attributes <abc:xyz /> <abc:yyz>This is some text</abc:yyz>
Stand-Alone Tags Stand-alone tags are used when you want to insert output in a specific position in the JSP For example: Inserts a formatted number: Displays the shopping cart: <fmt:formatNumber … /> <books4u:ShoppingCart … />
Containing Tags Containing tags can be used for: Filtering: Iteration: Conditional execution: <xlate:translate>Translate this</xlate:translate> <filter:filterContent rating=&quot;VIP&quot;> … </filter:filterContent> <c:forEach> and <x:forEach> <db:executeQuery sql=&quot;select…&quot;>…</db:executeQuery> <c:if>, <c:choose>, <c:when> <security:auth role=&quot;admin&quot;> … </security:auth>
A Simple Tag Example Let’s create a simple tag file (the standard hello world example): Put this code in the file  hello.tag  in  /WEB-INF/tags Using the tag: <%@ tag body-content=&quot;empty&quot; %> <h1>Hello, world!</h1> <%@ taglib prefix=&quot;my&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <my:hello />
Hello Tag Live Demo
Attributes in Tags <%@ tag body-content=&quot;empty&quot; %> <%@ attribute name=&quot;min&quot; required=&quot;false&quot; %>  <%@ attribute name=&quot;max&quot; required=&quot;false&quot; %>  <% double dMin = 0.0; if (min != null) { dMin = Double.parseDouble(min); } double dMax = 1.0; if (max != null) { dMax = Double.parseDouble(max); } double number =  (Math.random()*(dMax-dMin)) + dMin; %> <%= number %>
Random Tag Live Demo
JSTL in Tags <%@ tag body-content=&quot;empty&quot; %> <%@ attribute name=&quot;firstName&quot; required=&quot;false&quot; %>  <%@ taglib prefix=&quot;c“ uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> <c:if test='${empty firstName}'> <h1>Hello,  guest !</h1> </c:if> <c:if test='${not empty firstName}'> <h1>Hello, <c:out value='${firstName}' />!</h1> </c:if>
Body Content in Tags The following tag uses the body content while iterating: Scriptless means that <% … %> are not allowed <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> <%@ attribute name=&quot;count&quot; required=&quot;true&quot; %> <c:forEach begin=&quot;1&quot; end=&quot;${count}&quot;> <jsp:doBody /> </c:forEach>
A Test JSP <%-- Include all tags from /WEB-INF/tags --%> <%@ taglib prefix=&quot;my&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <html> <body> <%-- Use the tag &quot;repeat.tag&quot; --%> <my:repeat count=&quot;10&quot;> <div>Hello!</div> </my:repeat> </body> </html>
Repeat Tag Live Demo
Exporting Variables from Tags The same tag can be modified to export a current index variable: <%@ tag body-content=&quot;scriptless&quot; %> <%@ attribute name=&quot;numTimes&quot; required=&quot;true&quot; %> <%@ attribute name=&quot;var&quot; rtexprvalue=&quot;false&quot; required=&quot;true&quot; %> <%@ variable name-from-attribute=&quot;var&quot; alias=&quot;i&quot; scope=&quot;NESTED&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <c:forEach begin='1' end='${numTimes}' var='i'> <jsp:doBody /> </c:forEach>
A Test JSP <%@ taglib prefix=&quot;tag_examples&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <html> <body> <tag_examples:repeat_var var='index' numTimes= '5' > <div>Hello #<c:out value='${index}' />!</div> </tag_examples:repeat_var> </body> </html>
Modifying Body Content in Tags We can store (and manipulate) the body content, instead of outputting it directly: <%@ tag body-content=&quot;scriptless&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <jsp:doBody var='body' /> <c:forTokens var='token' items='${body}' delims=' '> <div><c:out value='${token}' /></div> </c:forTokens>
A Test JSP <%@ taglib prefix=&quot;tag_examples&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <html> <body> <p> The quick brown fox jumped over the lazy dog. </p> <p> <tag_examples:tokenize> The quick brown fox jumped over the lazy dog. </tag_examples:tokenize> </p> </body> </html>
JSP Custom Tags Questions?
Problems Create a JSP/custom tags based web site with master pages and menu navigation that allows: Adding numbers Printing all numbers in range 1..100 Both operations should be available from different JSP pages that reuse the master page layout.
Problems (2) Create a JSP/custom tag for displaying a given collection of Books in a table. Every book is an object from a Book class with the following properties: ISBN Book title Author name Simple output:

More Related Content

JSP Custom Tags

  • 1. JSP Custom Tags Svetlin Nakov National Academy for Software Development academy.devbg.org Creating and Using Custom Tags
  • 2. Contents Tag Files Types of Tag Files JSTL in Tags Exporting Variables from Tags Modifying Body Content in Tags
  • 3. Tag Files Tag files let you define your own custom JSP tags You should use tag files when: You want to modularize a part of a JSP’s output You want to perform some behavior in many places (reusable) This behavior can be presentation or application logic <books4u:shoppingCart currency=&quot;EUR&quot; />
  • 4. Types of Tag Files Tag files come in two types: Stand-alone tags Tags containing other tags and text Both types of tags can have any number of attributes <abc:xyz /> <abc:yyz>This is some text</abc:yyz>
  • 5. Stand-Alone Tags Stand-alone tags are used when you want to insert output in a specific position in the JSP For example: Inserts a formatted number: Displays the shopping cart: <fmt:formatNumber … /> <books4u:ShoppingCart … />
  • 6. Containing Tags Containing tags can be used for: Filtering: Iteration: Conditional execution: <xlate:translate>Translate this</xlate:translate> <filter:filterContent rating=&quot;VIP&quot;> … </filter:filterContent> <c:forEach> and <x:forEach> <db:executeQuery sql=&quot;select…&quot;>…</db:executeQuery> <c:if>, <c:choose>, <c:when> <security:auth role=&quot;admin&quot;> … </security:auth>
  • 7. A Simple Tag Example Let’s create a simple tag file (the standard hello world example): Put this code in the file hello.tag in /WEB-INF/tags Using the tag: <%@ tag body-content=&quot;empty&quot; %> <h1>Hello, world!</h1> <%@ taglib prefix=&quot;my&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <my:hello />
  • 9. Attributes in Tags <%@ tag body-content=&quot;empty&quot; %> <%@ attribute name=&quot;min&quot; required=&quot;false&quot; %> <%@ attribute name=&quot;max&quot; required=&quot;false&quot; %> <% double dMin = 0.0; if (min != null) { dMin = Double.parseDouble(min); } double dMax = 1.0; if (max != null) { dMax = Double.parseDouble(max); } double number = (Math.random()*(dMax-dMin)) + dMin; %> <%= number %>
  • 11. JSTL in Tags <%@ tag body-content=&quot;empty&quot; %> <%@ attribute name=&quot;firstName&quot; required=&quot;false&quot; %> <%@ taglib prefix=&quot;c“ uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> <c:if test='${empty firstName}'> <h1>Hello, guest !</h1> </c:if> <c:if test='${not empty firstName}'> <h1>Hello, <c:out value='${firstName}' />!</h1> </c:if>
  • 12. Body Content in Tags The following tag uses the body content while iterating: Scriptless means that <% … %> are not allowed <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> <%@ attribute name=&quot;count&quot; required=&quot;true&quot; %> <c:forEach begin=&quot;1&quot; end=&quot;${count}&quot;> <jsp:doBody /> </c:forEach>
  • 13. A Test JSP <%-- Include all tags from /WEB-INF/tags --%> <%@ taglib prefix=&quot;my&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <html> <body> <%-- Use the tag &quot;repeat.tag&quot; --%> <my:repeat count=&quot;10&quot;> <div>Hello!</div> </my:repeat> </body> </html>
  • 15. Exporting Variables from Tags The same tag can be modified to export a current index variable: <%@ tag body-content=&quot;scriptless&quot; %> <%@ attribute name=&quot;numTimes&quot; required=&quot;true&quot; %> <%@ attribute name=&quot;var&quot; rtexprvalue=&quot;false&quot; required=&quot;true&quot; %> <%@ variable name-from-attribute=&quot;var&quot; alias=&quot;i&quot; scope=&quot;NESTED&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <c:forEach begin='1' end='${numTimes}' var='i'> <jsp:doBody /> </c:forEach>
  • 16. A Test JSP <%@ taglib prefix=&quot;tag_examples&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <html> <body> <tag_examples:repeat_var var='index' numTimes= '5' > <div>Hello #<c:out value='${index}' />!</div> </tag_examples:repeat_var> </body> </html>
  • 17. Modifying Body Content in Tags We can store (and manipulate) the body content, instead of outputting it directly: <%@ tag body-content=&quot;scriptless&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <jsp:doBody var='body' /> <c:forTokens var='token' items='${body}' delims=' '> <div><c:out value='${token}' /></div> </c:forTokens>
  • 18. A Test JSP <%@ taglib prefix=&quot;tag_examples&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %> <html> <body> <p> The quick brown fox jumped over the lazy dog. </p> <p> <tag_examples:tokenize> The quick brown fox jumped over the lazy dog. </tag_examples:tokenize> </p> </body> </html>
  • 19. JSP Custom Tags Questions?
  • 20. Problems Create a JSP/custom tags based web site with master pages and menu navigation that allows: Adding numbers Printing all numbers in range 1..100 Both operations should be available from different JSP pages that reuse the master page layout.
  • 21. Problems (2) Create a JSP/custom tag for displaying a given collection of Books in a table. Every book is an object from a Book class with the following properties: ISBN Book title Author name Simple output:

Editor's Notes

  1. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  2. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  3. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  4. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  5. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  6. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  7. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  8. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  9. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  10. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  11. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  12. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  13. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  14. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  15. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  16. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  17. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  18. * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##