| |
- Client JSP using the taglib
- Tag class definition (HelloTag.java)
- Tag configuration (hello.tld)
Using a taglib in a JSP file has two steps. First, the JSP needs to
define the prefix and the tld (tag library descriptor)
for the tag.
Normally, JSP copies bytes from the .jsp to the browser without
interpreting it. The taglib directive tells the JSP engine to
treat elements starting with the prefix as JSP extension tags.
hello.jsp
<%@ taglib prefix="ct" uri="WEB-INF/hello.tld" %>
Message: <ct:hello/>
|
Many simple tags can just extend TagSupport. The hello tag
implements doStartTag. When the hello tag starts, the JSP will
execute doStartTag. In this case, it'll print "hello, world".
Since the content of the hello tag doesn't matter, HelloTag
returns SKIP_BODY. Empty tags will generally
return SKIP_BODY.
HelloTag.java
package test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
public int doStartTag() throws JspException
{
try {
pageContext.getOut().println("hello, world");
} catch (IOException e) {
}
return SKIP_BODY;
}
}
|
Finally, you need to configure the tag in a .tld (tag library
descriptor) file. The .tld matches the tag name hello with the tag
class test.HelloTag.
WEB-INF/hello.tld
<taglib>
<tag>
<name>hello</name>
<tagclass>test.HelloTag</tagclass>
</tag>
</taglib>
|
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|