[ Team LiB ] Previous Section Next Section

Recipe 22.14 Using a Custom Tag Associated with a Tag File

Problem

You want to use a custom tag associated with a tag file.

Solution

Use the taglib directive in the JSP, before the code that uses the tag file related tag.

Discussion

The taglib directive identifies the tag with its tagdir attribute, which is the web application path to the tags directory. Example 22-10 uses the tag from a tag file stored at /WEB-INF/tags/logo.tag.

The tag name in the JSP is the same as the tag filename, without the .tag extension. The prefix attribute represents the custom tag's namespace, so the entire tag is used in the JSP as "<cbck:logo heading=...> ...tag content...</cbck:logo>."

Example 22-10. A JSP uses a tag defined in a tag file
<%@ taglib prefix="cbck" tagdir="/WEB-INF/tags" %>

<html>
<head><title>Me Casa Su Casa</title></head>
<body>

<% session.setAttribute("imgDir",(request.getContextPath( ) + "/images/")); %>

<cbck:logo heading="<%=request.getParameter(\"level\") %>" image=
  "stamp.gif" width="42" height="54">
Thanks for visiting here ...
</cbck:logo>

Here's all the other stuff this page contains...
</body>
</html>

I use the same basic logo tag throughout this chapter to illustrate the various custom-tag syntax differences. See Recipe 22.2 for details on the logo tag itself.


See Also

The JSP 2.0 specification web page: http://jcp.org/en/jsr/detail?id=152; Recipe 22.2 and Recipe 22.3 on creating TLD files for tag libraries; Recipe 22.4 and Recipe 22.5 on packaging a tag library in a web application; Recipe 22.6 on using the custom tag in a JSP; Recipe 22.7 on handling exceptions in tags; Recipe 22.8 and Recipe 22.9 on creating a simple tag handler; Recipe 22.10 on using the simple tag handler in a JSP; Recipe 22.11-Recipe 22.13 on setting up a JSP tag file; Recipe 22.15 on adding a listener class to a tag library; the custom tag sections of Hans Bergsten's JavaServer Pages, Third Edition (O'Reilly).

    [ Team LiB ] Previous Section Next Section