Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JSP Custom Tags

10,351 views

Published on

Short presentation about creating custom tags for JavaServer Pages

Published in: Technology
  • Be the first to comment

JSP Custom Tags

  1. 1. JSP Custom Tags Svetlin Nakov National Academy for Software Development academy.devbg.org Creating and Using Custom Tags
  2. 2. Contents <ul><li>Tag Files </li></ul><ul><ul><li>Types of Tag Files </li></ul></ul><ul><li>JSTL in Tags </li></ul><ul><li>Exporting Variables from Tags </li></ul><ul><li>Modifying Body Content in Tags </li></ul>
  3. 3. Tag Files <ul><li>Tag files let you define your own custom JSP tags </li></ul><ul><li>You should use tag files when: </li></ul><ul><ul><li>You want to modularize a part of a JSP’s output </li></ul></ul><ul><ul><li>You want to perform some behavior in many places (reusable) </li></ul></ul><ul><ul><li>This behavior can be presentation or application logic </li></ul></ul><books4u:shoppingCart currency=&quot;EUR&quot; />
  4. 4. Types of Tag Files <ul><li>Tag files come in two types: </li></ul><ul><ul><li>Stand-alone tags </li></ul></ul><ul><ul><li>Tags containing other tags and text </li></ul></ul><ul><li>Both types of tags can have any number of attributes </li></ul><abc:xyz /> <abc:yyz>This is some text</abc:yyz>
  5. 5. Stand-Alone Tags <ul><li>Stand-alone tags are used when you want to insert output in a specific position in the JSP </li></ul><ul><li>For example: </li></ul><ul><ul><li>Inserts a formatted number: </li></ul></ul><ul><ul><li>Displays the shopping cart: </li></ul></ul><fmt:formatNumber … /> <books4u:ShoppingCart … />
  6. 6. Containing Tags <ul><li>Containing tags can be used for: </li></ul><ul><ul><li>Filtering: </li></ul></ul><ul><ul><li>Iteration: </li></ul></ul><ul><ul><li>Conditional execution: </li></ul></ul><ul><ul><ul><li><xlate:translate>Translate this</xlate:translate> </li></ul></ul></ul><ul><ul><ul><li><filter:filterContent rating=&quot;VIP&quot;> … </filter:filterContent> </li></ul></ul></ul><ul><ul><ul><li><c:forEach> and <x:forEach> </li></ul></ul></ul><ul><ul><ul><li><db:executeQuery sql=&quot;select…&quot;>…</db:executeQuery> </li></ul></ul></ul><ul><ul><ul><li><c:if>, <c:choose>, <c:when> </li></ul></ul></ul><ul><ul><ul><li><security:auth role=&quot;admin&quot;> … </security:auth> </li></ul></ul></ul>
  7. 7. A Simple Tag Example <ul><li>Let’s create a simple tag file (the standard hello world example): </li></ul><ul><li>Put this code in the file hello.tag in /WEB-INF/tags </li></ul><ul><li>Using the tag: </li></ul><%@ tag body-content=&quot;empty&quot; %> <h1>Hello, world!</h1> <%@ taglib prefix=&quot;my&quot; tagdir=&quot;/WEB-INF/tags&quot; %> <my:hello />
  8. 8. Hello Tag Live Demo
  9. 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 %>
  10. 10. Random Tag Live Demo
  11. 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. 12. Body Content in Tags <ul><li>The following tag uses the body content while iterating: </li></ul><ul><li>Scriptless means that <% … %> are not allowed </li></ul><%@ 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. 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>
  14. 14. Repeat Tag Live Demo
  15. 15. Exporting Variables from Tags <ul><li>The same tag can be modified to export a current index variable: </li></ul><%@ 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. 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. 17. Modifying Body Content in Tags <ul><li>We can store (and manipulate) the body content, instead of outputting it directly: </li></ul><%@ 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. 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. 19. JSP Custom Tags <ul><li>Questions? </li></ul>
  20. 20. Problems <ul><li>Create a JSP/custom tags based web site with master pages and menu navigation that allows: </li></ul><ul><ul><ul><li>Adding numbers </li></ul></ul></ul><ul><ul><ul><li>Printing all numbers in range 1..100 </li></ul></ul></ul><ul><ul><li>Both operations should be available from different JSP pages that reuse the master page layout. </li></ul></ul>
  21. 21. Problems (2) <ul><li>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: </li></ul><ul><ul><ul><li>ISBN </li></ul></ul></ul><ul><ul><ul><li>Book title </li></ul></ul></ul><ul><ul><ul><li>Author name </li></ul></ul></ul><ul><ul><li>Simple output: </li></ul></ul>

×