In this JavaEE JSP Custom Tag Tutorial, we will learn how to use JspFragment class to support JSP Fragment attribute.
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 57 58 |
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) { //Use Existing doTag method to Print Type and Price Title BookTitle = (Title) obj; //FragAT 03: Allow Customization only for Title //via Fragment Attribute jc.setAttribute("BookTitle", BookTitle); print_title.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 21 22 |
<!--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> <body-content>empty</body-content> <attribute> <name>books</name> <type>java.util.ArrayList</type> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <!-- FragAT 04: Add the JSP Fragment Attribute --> <attribute> <name>print_title</name> <fragment>true</fragment> </attribute> </tag> </taglib> |
DisplayBooks4.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 26 27 28 29 30 31 32 33 |
<!-- 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" %> <%@ 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> <!-- Sample 07: Entry for our custom Tag Library --> <H1>Using the custom Tag to List Book Titles</H1> <!-- FragAT 05: Provide how the Fragment should Look. Snippet Sample 08 is replaced --> <My:ListBooks books="${BookTitles}" > <jsp:attribute name="print_title"> <p style="font-size: 36px; color: gold; font-style: italic; font-family: 'Arial Narrow'; background-color: green;"> ${BookTitle.bookTitle} </p> </jsp:attribute> </My:ListBooks> </body> </html> |
Categories: JSP