Java for the WWW November 2012Slide [email protected] Tag Libraries What are they?...

13
Java for the WWW November 2012 Slide 1 [email protected] Tag Libraries What are they? Custom libraries Custom tags – see examples in following slides Standard Tag Library (JSTL) – recommend that you read the following: http://today.java.net/pub/a/today/2003/10/07/js tl1.html http://today.java.net/pub/a/today/2003/11/27/js tl2.html Purpose and benefits for JSP web development Lot of development and includes the AJAX technology but this is outside the scope of this module For further reading on Tag libraries – here is nice tutorial to back up the following notes <link>

Transcript of Java for the WWW November 2012Slide [email protected] Tag Libraries What are they?...

Page 1: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

November 2012 Slide [email protected]

Tag Libraries

• What are they?– Custom libraries

• Custom tags – see examples in following slides

– Standard Tag Library (JSTL) – recommend that you read the following:

• http://today.java.net/pub/a/today/2003/10/07/jstl1.html• http://today.java.net/pub/a/today/2003/11/27/jstl2.html

• Purpose and benefits for JSP web development– Lot of development and includes the AJAX technology but

this is outside the scope of this module

• For further reading on Tag libraries – here is nice tutorial to back up the following notes <link>

Page 2: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Custom tags

• Custom tags are dynamic page elements.• This means they can provide functionality to a

web page using a syntax similar to normal HTML tags.

• The functionality is up to you…• They can provide an alternative (or an

additional) mechanism to JavaBeans for dynamic scripting.

Page 3: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

What do you need?

• There are two components to a tag:– A Java tag handler class that implements the tag’s

functionality.– A Tag Library Descriptor (TLD) in the form of an

XML file describing the tag.

• By including this tag on a JSP page you have access to its functionality.

Page 4: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Java handler class

• Yes, you have to code the functionality in Java.

• So why bother? Why not just create a JavaBean instead?– For standard web page scripters, tags are more

familiar and provide a simpler mechanism.– For web application developers, JavaBeans

integrate more fully with the full application environment so are more powerful.

• Your choice….

Page 5: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Making the handler

• You Java class needs to extend the TagSupport class.

• This class is only available in the Enterprise version of the Java Development Kit (from version 1.3.1).

• Or, the Java Web Services Developer Pack.

• However, you just need the lib/j2ee.jap file (in the case of J2EE) or the lib/servlet.jar for the JWSDP.

• You can then extract the javax.servlet.jsp.tagext class from the .jar file (using WinZip etc.) and put it into folder containing your tag classes – see screen grab on next powerpoint

Page 6: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Page 7: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Making the handler

• Once you have your custom tag Java code, you need to compile it and put the resultant .class file in your WEB-INF/classes folder.

• For the examples, this would go in:– WEB-INF/classes/mytags

Page 8: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Example handler

Page 9: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Tag Library Descriptor

• A TLD is a (fairly) simple XML file that describes a set of custom tags.

• You can put as many or as few custom tags in a TLD and more than one TLD can be used in a single project.

• In principle, you could create many useful tags and zip them into one .jar file for easy re-usability.

Page 10: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

Slide [email protected]

Example Tag Descriptor

Page 11: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Using your custom tag

• To use your custom tag, you need a JSP page.

• You need to tell your JSP where to find your tag library so you use a page directive:

<%@ taglib uri=“WEB-INF/nameofdescriptor.tld” prefix=“something” %>

• Anywhere in your JSP page, you can then use your custom tag(s):

Page 12: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

Example with a JSP

Page 13: Java for the WWW November 2012Slide 1j.c.westlake@staffs.ac.uk Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.

Java for the WWW

March 2009 Slide [email protected]

The use of tag libraries is all about...

• Re-usability

• Defining things once and making them available for use in more than one place

• Simplification

• Bespoke JSP work

• Or using existing tag libraries available for use - See the following

Jsptags.comJspinJakata Apache Taglibs project