In this JavaEE JSP Tutorial, we will see how to create JSP Tag File (.tag) and use it to render a specific Tag. This example, replaces the fragment attribute created in the previous example.
BookTitle.tag
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!-- TagFile 01: Create a Tag File --> <%@ tag language="java" pageEncoding="ISO-8859-1"%> <%@ attribute name="bt" type="tube.codingexamples.bean.Title" required="true" rtexprvalue="true" %> <h2 style="font-size: medium; font-family: Verdana; vertical-align: middle; text-align: left; color: #3300ff"> ${bt.bookTitle} </h2> <!-- For Demo <p>[${bt.type}] </p> <p>Price => ${bt.bookPrice} </p> --> |
ListBookTitlesTag.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package tube.codingexamples.jtags; import java.io.IOException; import java.util.ArrayList; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; import tube.codingexamples.bean.Title; public class ListBookTitlesTag extends SimpleTagSupport { //Sample 01: Define the books as a collection @SuppressWarnings("rawtypes") ArrayList books; //FragAT 01: TitlePrice JspFragment print_title; //FragAT 02: Setter method of the Fragment public void setPrint_title(JspFragment print_title) { this.print_title = print_title; } //Sample 02: Set the book titles @SuppressWarnings("rawtypes") public void setBooks(ArrayList books) { this.books = books; } //Sample 03: Do the Html Tag to represent the Books Titles @Override public void doTag() throws JspException, IOException { //3.1: Get output stream for writing html content JspContext jc = getJspContext(); JspWriter out = jc.getOut(); //3.2: Iterate the Book collection & form Html for(Object obj : books) { //TagFile 06: Use Tag File for Rendering //the Book Title Title BookTitle = (Title) obj; jc.setAttribute("OneTitle", BookTitle); getJspBody().invoke(null); //Use Existing doTag method to Print Type and Price out.println("<p>"); out.println(BookTitle.getType() + ", "); out.println(BookTitle.getBookPrice()); out.println("</p>"); } } } |
BookTitles.tld
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!--Sample 04: Define the Tag Definition --> <taglib version="2.0"> <short-name>List of Books</short-name> <tlib-version>2.0</tlib-version> <tag> <name>ListBooks</name> <tag-class>tube.codingexamples.jtags.ListBookTitlesTag</tag-class> <!-- //TagFile 04: Change Body Content as Scriptless. Prev. Line Commented --> <body-content>scriptless</body-content> <attribute> <name>books</name> <type>java.util.ArrayList</type> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <!-- //TagFile 05: Moved the Fragement to Tagfile--> </tag> </taglib> |
DisplayBooks5.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!-- Sample 05: Entry for Core Tag Library --> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- Sample 06: Entry for our custom Tag Library --> <%@ taglib uri="http://www.tube.codingexamples.jtags" prefix="My" %> <!-- TagFile 02: Specify the Tag File Location --> <%@ taglib tagdir="/WEB-INF/tags" prefix="tg" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- TagFile 03: Custom Tag Replacing the Fragment with Tag File --> <My:ListBooks books="${BookTitles}" > <tg:BookTitle bt="${OneTitle}" /> </My:ListBooks> </body> </html> |
Categories: JSP