930 likes | 1.16k Views
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
E N D
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 JSP Custom Tags
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
Tags? Actions? Huh? A brief introduction to custom tags. JSP Custom Tags
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
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
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
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
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
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
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
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
Tag Interface All tag handler classes must implement the Tag interface. JSP Custom Tags
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
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
Return Values • int doStartTag() • SKIP_BODY • EVAL_BODY_INCLUDE • int doEndTag() • SKIP_PAGE • EVAL_PAGE JSP Custom Tags
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
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
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
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
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
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
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
Example Background • root appender1 • category1 appender2 • category2 appender3 • category 3 appender4 JSP Custom Tags
Example Simple Tag Demo JSP Custom Tags
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
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
Tag Attribute Setters • <log:logMessage • message=“test msg”/> • would call • setMessage(String pMessage) • with the String value “test msg” JSP Custom Tags
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
Tag Attribute Setters • <log:initLog override=“true”/> • would call • setOverride(boolean pOverride) • with the boolean value true. JSP Custom Tags
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
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
Call Sequence Diagram <%@ taglib …%> <log:category priority=“fatal” 1-> setPriority() > 2-> doStartTag() Body contents. </log:category>3-> doEndTag() JSP Custom Tags
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
<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
Example Background • root appender1 • category1 appender2 • category2 appender3 • category 3 appender4 JSP Custom Tags
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
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
Example Tag Attribute Demo JSP Custom Tags
Tags and Scripting Variables Tags can introduce variables that can be used later in a JSP scriptlet or another custom tag. JSP Custom Tags
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
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
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
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
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
TLD Addition for TEI Class • You must also add a <teiclass/> • element to your TLD: • <teiclass> • com.foo.CategoryTagExtraInfo • </teiclass> JSP Custom Tags
Example Background SimpleAppenderTag CategoryTag LogMessageTag <log:simpleAppender id="myAppender" … /> <log:category id="testCat" appender="myAppender“ … /> <log:logMessage categoryName="testCat" … /> JSP Custom Tags
Example Scripting Variable Demo JSP Custom Tags
BREAK 15 minutes. JSP Custom Tags
Body Tags Body Tags process their tag body contents. The body contents can contain JSP content. JSP Custom Tags