1 / 90

JSP Custom Tags

JSP Custom Tags. Keyton Weissinger Key Focus, Inc. keytonw@mindspring.com 27 March 2001 O’Reilly Conference on Enterprise Java Santa Clara, CA. Agenda, Part I. Tags? Actions? Huh? Tag Interface Tag Attributes Tags and Scripting Variables BREAK. Agenda, Part II. Body Tags

xaria
Download Presentation

JSP Custom Tags

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. JSP Custom Tags Keyton Weissinger Key Focus, Inc. keytonw@mindspring.com 27 March 2001 O’Reilly Conference on Enterprise Java Santa Clara, CA

  2. Agenda, Part I • Tags? Actions? Huh? • Tag Interface • Tag Attributes • Tags and Scripting Variables • BREAK JSP Custom Tags

  3. Agenda, Part II • Body Tags • Nested Tags • Iterating Tags • Tags and the JSP Container • Packaging and Deployment • Wrap-up and Q&A JSP Custom Tags

  4. Tags? Actions? Huh? A brief introduction to custom tags. JSP Custom Tags

  5. Definitions • A “tag” is an XML element that can be used in a JSP to incorporate functionality without using scriptlet code. • An “action” is the task performed by the tag. JSP Custom Tags

  6. Definitions, cont. • A “tag handler” is basically a JavaBean that implements the Tag interface and has property setter methods corresponding to each tag attribute. The JSP container uses this class to process the tag. • A “tag library” or “taglib” is a collection of one or more custom tags. JSP Custom Tags

  7. Tag Syntax • A custom tag consists of a start tag • with a taglib-identifying prefix (possibly • containing attributes), a body (which • can be empty) and an end tag: • <log:logMessage categoryName="testCat" priority="fatal"> • The body contents… • </log:logMessage> JSP Custom Tags

  8. Alternate Tag Syntax If a tag does not contain any body contents, it can be used like this: <log:logMessage categoryName="testCat" priority="fatal" message="test"/> JSP Custom Tags

  9. The <%@taglib%> Directive Before using a tag library, you must inform the JSP container about the tag library using the <%@taglib%> directive before the first use of a tag: <%@ taglib uri="/logtags" prefix="log" %> The taglib directive also allows you to specify a prefix to use with a given tag library in its use in your JSP. JSP Custom Tags

  10. Tag Libraries • One or more tags can be packaged together as a “tag library.” • A tag library is described in a tag library descriptor (.TLD) file. • Tags from different libraries can be called from the same JSP, but each must be declared separately with its own <%@taglib%> directive and must be uniquely identified with its own prefix. JSP Custom Tags

  11. What Can Tags Do? • A custom tag can: • Know about its JSP environment. • Perform simple tasks. • Perform formatting or other customization of output. • Introduce variables usable from scriptlets later in the JSP. • Work together either as nested pairs or separately. JSP Custom Tags

  12. Why Are Tags Important? • Simplification of complex tasks across multiple JSPs in a web application. • Complex code hiding. • Design tool incorporation. • Portable to any JSP container. • Incorporation of functionality into JSPs not using Java as the scripting language. JSP Custom Tags

  13. Tag Interface All tag handler classes must implement the Tag interface. JSP Custom Tags

  14. Tag Interface • public interface Tag { • void setPageContext(PageContext pc) • void setParent(Tag t) • Tag getParent() • int doStartTag() throws • JspException • int doEndTag() throws JspException • void release() • } JSP Custom Tags

  15. Tag Interface Constants • The Tag interface also includes the • following constants: • public final static int SKIP_BODY = 0; • public final static int EVAL_BODY_INCLUDE = 1; • public final static int SKIP_PAGE = 5; • public final static int EVAL_PAGE = 6; JSP Custom Tags

  16. Return Values • int doStartTag() • SKIP_BODY • EVAL_BODY_INCLUDE • int doEndTag() • SKIP_PAGE • EVAL_PAGE JSP Custom Tags

  17. TagSupport Class Additions In addition to those methods in Tag, the TagSupport class also provides the following: public static final Tag findAncestorWithClass(Tag from, Class klass) public void setId(String id) public String getId() public void setValue(String k, Object o) public Object getValue(String k) public void removeValue(String k) public Enumeration getValues() JSP Custom Tags

  18. Tag Library Descriptor Files • The JSP container generates code in the JSP implementation class for a tag according to information you set in the TLD. • The TLD is an XML document. • It contains a single <tag/> element for each tag in the library. JSP Custom Tags

  19. TLD Elements • The TLD has the following elements: • The <taglib/> element contains all the other elements in the TLD. • <tlibversion/> • <uri/> • <jspversion/> • <shortname/> JSP Custom Tags

  20. TLD Elements, cont. • The <taglib/> element contains a <tag/> • element for each tag in the library. The possible • sub-elements of <tag/> are as follows: • <name/> • <tagclass/> • <teiclass/> • <bodycontent/> • <info/> • <attribute/> JSP Custom Tags

  21. TLD Example <?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>logtags</shortname> <uri></uri> <info>Log4J Wrapper Tags</info> <tag> <name>initLog</name> <tagclass> com.oreilly.jsp.taglibs.logtags.InitLogTag </tagclass> <info>Logging system initialization tag.</info> </tag> </taglib> JSP Custom Tags

  22. Example Background • Log4j is an open source project. • Allows developers to fully control logging of an application. • Fully runtime configurable. • Gentle learning curve. • See http://jakarta.apache.org/log4j/ JSP Custom Tags

  23. Example Background • A category is a named entity that categorizes a part of the logging space. • An appender is a logging output destination. • A layout is a formatter for output to an appender. • Priority is the minimum level of messages to be logged for a category. JSP Custom Tags

  24. Example Background • root appender1 • category1 appender2 • category2 appender3 • category 3 appender4 JSP Custom Tags

  25. Example Simple Tag Demo JSP Custom Tags

  26. Tag Attributes A tag can have zero or more attributes. Attribute values provide information to the tag’s tag handler class at execution time. JSP Custom Tags

  27. Tag Attributes • Each attribute has a corresponding setXxxx() method. • Each attribute value included in the tag triggers a call to its corresponding setter. No particular order. • The setter called can be determined by capitalizing the first letter of the attribute name and adding the word “set” as a prefix. JSP Custom Tags

  28. Tag Attribute Setters • <log:logMessage • message=“test msg”/> • would call • setMessage(String pMessage) • with the String value “test msg” JSP Custom Tags

  29. Attribute Data Types • The signature of the attribute setter method in the tag handler class dictates the data type expected for that attribute. JSP Custom Tags

  30. Tag Attribute Setters • <log:initLog override=“true”/> • would call • setOverride(boolean pOverride) • with the boolean value true. JSP Custom Tags

  31. boolean or Boolean byte or Byte char or Character double or Double int or Integer float or Float long or Long Boolean.valueOf(String) Byte.valueOf(String) String.charAt(int) Double.valueOf(String) Integer.valueOf(String) Float.valueOf(String) Long.valueOf(String) Tag Attribute Value Conversion • Property Type • Conversion Method JSP Custom Tags

  32. JSP Container Call Sequence • The JSP container call sequence goes as • follows: • Instantiates tag handler. • Calls setup methods (setPageContext(), etc). • Calls each setXxxx() method depending on each attribute’s presence in the tag. • Calls doStartTag(). • Calls doEndTag(). JSP Custom Tags

  33. Call Sequence Diagram <%@ taglib …%> <log:category priority=“fatal” 1-> setPriority() > 2-> doStartTag() Body contents. </log:category>3-> doEndTag() JSP Custom Tags

  34. Runtime Attribute Values Attribute values can be the result of a runtime expression: <% Collection roomsCol = new ArrayList(); %> <log:logCollection collection="<%=roomsCol%>“/> NOTE: This is how to handle other data types. JSP Custom Tags

  35. <attribute/> Element There is an <attribute/> element in the TLD for each attribute for each tag: <attribute> <name>message</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> JSP Custom Tags

  36. Example Background • root appender1 • category1 appender2 • category2 appender3 • category 3 appender4 JSP Custom Tags

  37. Example Background • A log4j category’s “priority” setting • specifies the “level” required for a • message to be logged for a given • category: • Debug • Info • Warn • Error • Fatal JSP Custom Tags

  38. Example Background • Each category has methods corresponding to the various priorities. You use the appropriate method to log a message of a given priority for the category of interest: • myCat.debug(“Here’s a message.”); • myCat.info(“Here’s a message.”); • myCat.warn(“Here’s a message.”); • myCat.error(“Here’s a message.”); • myCat.fatal(“Here’s a message.”); JSP Custom Tags

  39. Example Tag Attribute Demo JSP Custom Tags

  40. Tags and Scripting Variables Tags can introduce variables that can be used later in a JSP scriptlet or another custom tag. JSP Custom Tags

  41. Tag-Introduced Scripting Variables • This mechanism allows your tag to • introduce a variable that you can use in • scriptlets later in the JSP: • <log:category id=“myCat”/> • <% • myCat.debug(“Hello!”); • %> JSP Custom Tags

  42. Introduction of Scripting Variables • To have your tag introduce a scripting • variable, you must perform the • following steps: • Somewhere in your tag handler class you must add your variable to one of the JSP scopes. • Create a TagExtraInfo class for your tag handler class and reference it in your TLD. JSP Custom Tags

  43. Creating a Scripting Variable <log:category id=“myCat”/> From TagSupport base class: void setId(String id) { this.id = id; } From your tag’s doStartTag() method: Category rootCat = Category.getRoot(); pageContext.setAttribute(id, rootCat, Page.APPLICATION_SCOPE); JSP Custom Tags

  44. TagExtraInfo Class public class CategoryTagExtraInfo extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { return new VariableInfo[] { new VariableInfo( data.getAttributeString("id"), "org.apache.log4j.Category", true, VariableInfo.AT_END) }; } } JSP Custom Tags

  45. Variable Scope Tags can introduce scripting variables with one of three scopes represented by the three VariableInfo constants, NESTED, AT_BEGIN, and AT_END. The following diagram explains these three scopes: NESTED AT_BEGIN AT_END doStartTag() doEndTag() JSP Custom Tags

  46. TLD Addition for TEI Class • You must also add a <teiclass/> • element to your TLD: • <teiclass> • com.foo.CategoryTagExtraInfo • </teiclass> JSP Custom Tags

  47. Example Background SimpleAppenderTag CategoryTag LogMessageTag <log:simpleAppender id="myAppender" … /> <log:category id="testCat" appender="myAppender“ … /> <log:logMessage categoryName="testCat" … /> JSP Custom Tags

  48. Example Scripting Variable Demo JSP Custom Tags

  49. BREAK 15 minutes. JSP Custom Tags

  50. Body Tags Body Tags process their tag body contents. The body contents can contain JSP content. JSP Custom Tags

More Related