creating custom jsp tagcreating custom jsp tag libraries ... servlets, jsp, jsf 2.0, struts, ajax,...

Download Creating Custom JSP TagCreating Custom JSP Tag Libraries ... Servlets, JSP, JSF 2.0, Struts, Ajax, GWT

If you can't read please download the document

Post on 26-Jul-2020

2 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • © 2010 Marty Hall

    Creating Custom JSP TagCreating Custom JSP Tag Libraries: The Basics

    Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/msajsp.html

    Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at your location.2

    © 2010 Marty Hall

    For live Java EE training, please see training courses at http://courses.coreservlets.com/. at http://courses.coreservlets.com/.

    Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo, Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),

    Java 5, Java 6, SOAP-based and RESTful Web Services, Spring, g Hibernate/JPA, and customized combinations of topics.

    Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial Available at public

    Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at your location.

    Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your

    organization. Contact hall@coreservlets.com for details.

  • Agenda

    • What are tags? Why use them?g y • Java-based tags

    – Components of a tag library – Basic tags – Tags that use attributes

    Tags that use body content– Tags that use body content – Tags that optionally use body content

    • JSP-based tags (tag files)JSP based tags (tag files) – Components of a tag library – Basic tags – Tags that use attributes – Tags that use body content

    4

    © 2010 Marty Hall

    Intro

    Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at your location.5

  • Uses of JSP Constructs

    • Scripting elements calling servletScripting elements calling servlet code directly

    • Scripting elements calling servlet Simple

    Application g g code indirectly (by means of utility classes) B• Beans

    • Servlet/JSP combo (MVC) MVC ith JSP i l• MVC with JSP expression language

    • Custom tags MVC ith b t t d

    Complex Application • MVC with beans, custom tags, and

    a framework like JSF 2.0 6

    Application

    Tag Examples Blah, blah, blah. Blah blah blah Blah, blah, blah. Blah, blah, blah.

    This is a very important message

    Blah, blah, blah.

    Hello World

    7

  • © 2010 Marty Hall

    Java-Based Tags

    Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at your location.8

    Components That Make Up a Tag LibraryTag Library

    • The Tag Handler Class – Java code that says what to output – Must implement javax.servlet.jsp.tagext.SimpleTag

    Usually extends SimpleTagSupport– Usually extends SimpleTagSupport – Goes in same directories as servlet class files and beans

    • The Tag Library Descriptor FileThe Tag Library Descriptor File – XML file describing tag name, attributes, and

    implementing tag handler class – Goes under WEB-INF

    • The JSP File I t t lib ( f i URL f d i t fil )– Imports a tag library (referencing URL of descriptor file)

    – Defines tag prefix – Uses tags9

  • Defining a Simple Tag Handler ClassClass

    • Extend the SimpleTagSupport class • Import needed packages

    – import javax.servlet.jsp.*; import javax servlet jsp tagext *;import javax.servlet.jsp.tagext.*; import java.io.*;

    • Override doTagg – Obtain the JspWriter with getJspContext().getOut() – Use the JspWriter to generate output – Code gets called at request time

    • Tag instances are not reused like servlet instances, so no worry about race conditions, even if you have instance y y variables

    10

    Defining a Simple Tag Handler Class: ExampleClass: Example

    package coreservlets.tags;

    import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*;p j import java.math.*; import coreservlets.Primes;

    public class SimplePrimeTag extends SimpleTagSupport { protected int length = 50;

    bli id d T () th J E ti IOE ti {public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); BigInteger prime = Primes nextPrime(Primes random(length));Primes.nextPrime(Primes.random(length));

    out.print(prime); }

    }11

  • Defining a Simple Tag Library DescriptorDescriptor

    • Start with XML header • Top-level element is taglib

    – Just use tlib-version and short-name as in example E h t d fi d b l t ith• Each tag defined by tag element with: – description, which gives short info. Optional.

    name which defines the base tag name– name, which defines the base tag name. – tag-class, which gives the fully qualified class name of

    the tag handler. – body-content, which specifies if tag is standalone or

    contains content between start and end tag. • You can have multiple tag entries in each TLD file• You can have multiple tag entries in each TLD file • Put TLD file somewhere under WEB-INF

    12

    TLD File for SimplePrimeTag

    1 01.0 csajsp-taglib

    Outputs 50-digit primesOutputs 50 digit primes simplePrime coreservlets.tags.SimplePrimeTag empty

    ...

    • Don’t memorize XML header and standard

    part; download and modify online versionpart; download and modify online version – The important thing is to know how to write tag entries – Place TLD file somewhere under WEB-INF13

  • Accessing Custom Tags From JSP FilesJSP Files

    • Import the tag library – Specify location of TLD file

    prefix csajsp %

    – Define a tag prefix (namespace) prefix="csajsp" %> • Use the tags

    – • Tag name comes from TLD file • Prefix comes from taglib directive

    j i l i /– E.g.,

    14

    Using simplePrime Tag ...

    Some 50-Digit Primes

  • Using simplePrime Tag: ResultResult

    16

    Assigning Attributes to Tags

    • Allowing tags like – /> • Tags are still standalone

    – No body content between start and end tagsNo body content between start and end tags

    17

  • Attributes: The Tag Handler ClassThe Tag Handler Class

    • Use of an attribute called attribute1 i l l i ll h d ll dsimply results in a call to a method called setAttribute1

    Attribute value is supplied to method as a String– Attribute value is supplied to method as a String • Example

    – To supportTo support

    add the following to tag handler class:

    bli id ib ( i l ) {public void setAttribute1(String value1) { doSomethingWith(value1);

    }18

    Attributes: PrimeTag.java package coreservlets.tags;

    public class PrimeTag extends SimplePrimeTag { public void setLength(String length) {p g g g try {

    this.length = Integer.parseInt(length); } catch(NumberFormatException nfe) {} catch(NumberFormatException nfe) {

    this.length = 50; }

    } }

    19

  • Attributes: The Tag Library Descriptor FileThe Tag Library Descriptor File

    • The tag element must contain a nested l tattribute element

    • The attribute element has three further- nested elementsnested elements – name, a required element that defines the case-sensitive

    attribute name. – required, a required element that stipulates whether the

    attribute must always be supplied (true) or is optional (false)(false).

    – rtexprvalue, an optional attribute that indicates whether the attribute value can be a JSP expression like p (true) or whether it must be a fixed string (false). The default value is false.

    20

    TLD File for PrimeTag ...

    ...

    Outputs an N-digit primeOutputs an N digit prime prime coreservlets.tags.PrimeTag emptyempty

    length falseequ ed a se / equ ed

    ...

    21

  • Using prime Tag ...

    Some N-Digit Primes

    20-digit: 40 di i j i l h 40 /40-digit:

    80-digit: Default (50-digit):

    22

    Using prime Tag: Result

    23

  • Including the Tag Body

    • Simplest tags –

    • Tags with attributes fi N 1 " l1" /–

    • Now – Scriptless JSP Content

    – Scriptless JSP ContentScriptless JSP Content

    24

    Including Tag Body: The Tag Handler ClassThe Tag Handler Class

    • Call getJspBody().invoke(null);g p y() ( ) public void doTag() throws ... {

    JspWriter out = getJspContext().getOut(); out.print("..."); getJspBody().invoke(null); out.print("...");p ( );

    }

    25

  • Including Tag Body: HeadingTag javaHeadingTag.java

    public class HeadingTag extends SimpleTagSupport { private String align;p g g private String bgColor; private String border; private Str

Recommended

View more >