1 / 27

Creating Custom JSP Tag Libraries

Creating Custom JSP Tag Libraries. http://java.sun.com/products/jsp/taglibraries.html#tutorials Taken from Core Servlets and JavaServer Pages from Prentice Hall and Sun Microsystems Press, http://www.coreservlets.com/. © 2000 Marty Hall; may be freely used or adapted.

tarak
Download Presentation

Creating Custom JSP Tag Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Custom JSP Tag Libraries • http://java.sun.com/products/jsp/taglibraries.html#tutorials • Taken from Core Servlets and JavaServer Pages • from Prentice Hall and Sun Microsystems Press, • http://www.coreservlets.com/. • © 2000 Marty Hall; • may be freely used or adapted.

  2. JSP & Komponenten Java Entwickler Anwendungs Logik Daten Präsentation DB Daten Zugriff WEB Designer

  3. Beans • <jsp:useBean id="wobs" class="jspcr.beans.weather.Observation"> • <jsp:setProperty • name="wobs" • property="airportCode" • value="<%= airportCode %>"/> • </jsp:useBean>

  4. Custom Tags Aktion starten • <jax:example /> Simple Tag • <jax:debug> Body Tag • …. • </jax:debug> • + Kompaktere Syntax • + Inhalte bearbeiten • Arbeitsaufwendiger Aktion starten Inhalt bearbeiten Aktion starten

  5. Installieren und starten • Customtags benötigen folgende Elemente: • csajsp-taglib.tld Taglib Definition • SimpleExample.jsp JSP • ExampleTag.java Java Klasse • Pfad

  6. Custom Tags • Ein Tag ist eine Klasse, die • javax.servlet.jsp.tagext.Tag Interface implementiert.

  7. Hello World Kochrezept • 1. File HelloWorldTag.java im ../tag Ordner erstellen • 2. import Anweisungen: • package com.jax.tag • import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*; • 3. Implementierung javax.servlet.jsp.tagext.Tag Interface: • public class HelloWorldTag implements Tag { • 4. Container Variablen; • private PageContext pageContext;private Tag parent; • 5. Constructor: • public HelloWorldTag(){ • super(); • }

  8. Hello World Kochrezept • 6. Diese Methode wird beim Start-Tag ausgeführt: • public int doStartTag() throws javax.servlet.jsp.JspTagException { • return SKIP_BODY; • } • 7. Diese Methode wird beim End-Tag ausgeführt: • public int doEndTag() throws javax.servlet.jsp.JspTagException { • try{ • pageContext.getOut().write("Hello World!"); • }catch(java.io.IOException e){ • throw new JspTagException("IO Error: " + e.getMessage()); • }return EVAL_PAGE; // Den Rest der Seite weiterverarbeiten.}

  9. Hello World Kochrezept • 8. Es braucht noch weitere Methoden: • public void release() {} • 9. … • public void setPageContext(final javax.servlet.jsp.PageContext pageContext) {this.pageContext=pageContext; • } • 10. … • public void setParent(final javax.servlet.jsp.tagext.Tag parent) {this.parent=parent;} • 11. Finally, this method has to be implemented: • public javax.servlet.jsp.tagext.Tag getParent() { • return parent; • }

  10. Einfacher (extend TagSupport) • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • public class ExampleTag extends TagSupport { • public int doStartTag() { • try { • JspWriter out = pageContext.getOut(); • out.print("Hello World" + • "(Hello World)"); • } catch(IOException ioe) { • System.out.println("Error in ExampleTag: " + ioe); • } • return(SKIP_BODY); • } • }

  11. pageContext Methoden • getOut • getRequest • getResponse • getServletContext • getSession • JspWriter out = pageContext.getOut();

  12. SimpleExample.jsp • <HTML><HEAD> • <%@ taglib uri="csajsp-taglib.tld" prefix="jax" %> • <TITLE><jax:example /></TITLE></HEAD> • <BODY> • <H1><jax:example /></H1> • <jax:example /> • </BODY> • </HTML>

  13. taglib.tld (ins gleiche Verzeichnis wie die JSP‘s) • <?xml version="1.0" encoding="ISO-8859-1" ?> • <!DOCTYPE taglib • PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" • "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> • <taglib> • <tlibversion>1.0</tlibversion> • <jspversion>1.1</jspversion> • <shortname>csajsp</shortname> • <uri></uri> • <info> • Beispiel Tags für die Vorlesung JAX, • http://www.ifi.unibas.ch • </info> • <tag> • <name>example</name> • <tagclass>customtag.tags.ExampleTag</tagclass> • <info>Simplest example: inserts one line of output</info> • <bodycontent>empty</bodycontent> // JSP Inhalt wird als JSP interpretiert • </tag> • </taglib>

  14. SimplePrimeTag.java • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • import java.math.*; • import customtag.Primes; • public class SimplePrimeTag extends TagSupport { • protected int len = 50; • public int doStartTag() { • try { • JspWriter out = pageContext.getOut(); • BigInteger prime = Primes.nextPrime(Primes.random(len)); • out.print(prime); • } catch(IOException ioe) { • System.out.println("Error generating prime: " + ioe); • } • return(SKIP_BODY); • } • }

  15. PrimeTag.java • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • import java.math.*; • import customtag.tags.*; • public class PrimeTag extends SimplePrimeTag { • public void setLength(String length) { • try { • len = Integer.parseInt(length); • } catch(NumberFormatException nfe) { • len = 50; • } • } • }

  16. taglib • <tag> • <name>simplePrime</name> • <tagclass>customtag.tags.SimplePrimeTag</tagclass> • <info>Outputs a random 50-digit prime.</info> • <bodycontent>empty</bodycontent> • </tag> • <tag> • <name>prime</name> • <tagclass>customtag.tags.PrimeTag</tagclass> • <info>Outputs a random N-digit prime.</info> • <bodycontent>empty</bodycontent> • <attribute> • <name>length</name> • <required>false</required> • </attribute> • </tag>

  17. HeadingTag.jsp I • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • public class HeadingTag extends TagSupport { • private String bgColor; // The one required attribute • private String color = null; • private String align="CENTER"; • private String fontSize="36"; • private String fontList="Arial, Helvetica, sans-serif"; • private String border="0"; • private String width=null; • public void setBgColor(String bgColor) { • this.bgColor = bgColor; • } • public void setColor(String color) { • this.color = color; • }

  18. HeadingTag.jsp II • public int doStartTag() { • try { • JspWriter out = pageContext.getOut(); • out.print("<TABLE BORDER=" + border + • " BGCOLOR=\"" + bgColor + "\"" + • " ALIGN=\"" + align + "\""); • if (width != null) { • out.print(" WIDTH=\"" + width + "\""); • } • out.print("><TR><TH>"); • out.print("<SPAN STYLE=\"" + • "font-size: " + fontSize + "px; " + • "font-family: " + fontList + "; "); • if (color != null) { • out.println("color: " + color + ";"); • } • out.print("\"> "); // End of <SPAN ...> • } catch(IOException ioe) { • System.out.println("Error in HeadingTag: " + ioe); • } • return(EVAL_BODY_INCLUDE); // Include tag body • }

  19. HeadingTag.jsp III • public int doEndTag() { • try { • JspWriter out = pageContext.getOut(); • out.print("</SPAN></TABLE>"); • } catch(IOException ioe) { • System.out.println("Error in HeadingTag: " + ioe); • } • return(EVAL_PAGE); // Continue with rest of JSP page • } • }

  20. HeadingExample.jsp • <HTML><HEAD><TITLE>Some Tag-Generated Headings</TITLE></HEAD> • <BODY><%@ taglib uri="csajsp-taglib.tld" prefix="jax" %> • <jax:heading bgColor="#C0C0C0"> • Default Heading • </jax:heading> • <P> • <jax:heading bgColor="BLACK" color="WHITE"> • White on Black Heading • </jax:heading> • <P> • <jax:heading bgColor="#EF8429" fontSize="60" border="5"> • Large Bordered Heading • </jax:heading> • <P> • <jax:heading bgColor="CYAN" width="100%"> • Heading with Full-Width Background • </jax:heading> • <P> • <jax:heading bgColor="CYAN" fontSize="60" • fontList="Brush Script MT, Times, serif"> • Heading with Non-Standard Font • </jax:heading> • <P></BODY></HTML>

  21. Debug.jsp • <HTML><HEAD><TITLE>Using the Debug Tag</TITLE></HEAD> • <BODY> • <H1>Using the Debug Tag</H1> • <%@ taglib uri="csajsp-taglib.tld" prefix="jax" %> • Top of regular page. Blah, blah, blah. Yadda, yadda, yadda. • <P> • <jax:debug> • <B>Debug:</B> • <UL> • <LI>Current time: <%= new java.util.Date() %> • <LI>Requesting hostname: <%= request.getRemoteHost() %> • <LI>Session ID: <%= session.getId() %> • </UL> • </jax:debug> • <P>Bottom of regular page. Blah, blah, blah. Yadda, yadda, yadda. • </BODY></HTML>

  22. DebugTag.java • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • import javax.servlet.*; • public class DebugTag extends TagSupport { • public int doStartTag() { • ServletRequest request = pageContext.getRequest(); • String debugFlag = request.getParameter("debug"); • if ((debugFlag != null) && • (!debugFlag.equalsIgnoreCase("false"))) { • return(EVAL_BODY_INCLUDE); • } else { • return(SKIP_BODY); • } • } • }

  23. FilterTag.java • package customtag.tags; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.*; • import customtag.*; • public class FilterTag extends BodyTagSupport { • public int doAfterBody() { • BodyContent body = getBodyContent(); • String filteredBody = ServletUtilities.filter(body.getString()); • try { • JspWriter out = body.getEnclosingWriter(); • out.print(filteredBody); • } catch(IOException ioe) { • System.out.println("Error in FilterTag: " + ioe); • } • return(SKIP_BODY); • } • }

  24. FilterExample.jsp • <HTML><HEAD><TITLE>HTML Logical Character Styles</TITLE> • <BODY> • <H1>HTML Logical Character Styles</H1> • Physical character styles (B, I, etc.) are rendered consistently • in different browsers. Logical character styles, however, • may be rendered differently by different browsers. • Here's how your browser • (<%= request.getHeader("User-Agent") %>) • renders the HTML 4.0 logical character styles: • <P> • <%@ taglib uri="csajsp-taglib.tld" prefix="jax" %> • <TABLE BORDER=1 ALIGN="CENTER"> • <TR CLASS="COLORED"><TH>Example<TH>Result • <TR>

  25. FilterExample.jsp • <TD><PRE><jax:filter> • <EM>Some emphasized text.</EM><BR> • <STRONG>Some strongly emphasized text.</STRONG><BR> • <CODE>Some code.</CODE><BR> • <SAMP>Some sample text.</SAMP><BR> • <KBD>Some keyboard text.</KBD><BR> • <DFN>A term being defined.</DFN><BR> • <VAR>A variable.</VAR><BR> • <CITE>A citation or reference.</CITE> • </jax:filter></PRE> • <TD><EM>Some emphasized text.</EM><BR> • <STRONG>Some strongly emphasized text.</STRONG><BR> • <CODE>Some code.</CODE><BR> • <SAMP>Some sample text.</SAMP><BR> • <KBD>Some keyboard text.</KBD><BR> • <DFN>A term being defined.</DFN><BR> • <VAR>A variable.</VAR><BR> • <CITE>A citation or reference.</CITE> • </TABLE></BODY></HTML>

  26. Taglibs

  27. Praktikum • Frohe Festtage • Bean Übung beenden • Beispiele Customtags durchspielen • 3. Eine Taglib von Apache austesten • http://jakarta.apache.org/taglibs/index.html

More Related