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="EUR" />
- 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="VIP"> … </filter:filterContent> <c:forEach> and <x:forEach> <db:executeQuery sql="select…">…</db:executeQuery> <c:if>, <c:choose>, <c:when> <security:auth role="admin"> … </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="empty" %> <h1>Hello, world!</h1> <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> <my:hello />
- 9. Attributes in Tags <%@ tag body-content="empty" %> <%@ attribute name="min" required="false" %> <%@ attribute name="max" required="false" %> <% 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="empty" %> <%@ attribute name="firstName" required="false" %> <%@ taglib prefix="c“ uri="http://java.sun.com/jsp/jstl/core" %> <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="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="count" required="true" %> <c:forEach begin="1" end="${count}"> <jsp:doBody /> </c:forEach>
- 13. A Test JSP <%-- Include all tags from /WEB-INF/tags --%> <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> <html> <body> <%-- Use the tag "repeat.tag" --%> <my:repeat count="10"> <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="scriptless" %> <%@ attribute name="numTimes" required="true" %> <%@ attribute name="var" rtexprvalue="false" required="true" %> <%@ variable name-from-attribute="var" alias="i" scope="NESTED" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <c:forEach begin='1' end='${numTimes}' var='i'> <jsp:doBody /> </c:forEach>
- 16. A Test JSP <%@ taglib prefix="tag_examples" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <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="scriptless" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <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="tag_examples" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <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>
- 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
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
- * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##