10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

To use a custom tag in a JSP page, you must<br />

■ Declare the tag library containing the tag<br />

■ Make the tag library implementation available to the web application<br />

See Chapter 8, “Custom Tags in JSP Pages,” for detailed information on the different types of<br />

tags and how to implement tags.<br />

DeclaringTag Libraries<br />

To declare that a JSP page will use tags defined in a tag library, you include a taglib directive in<br />

the page before any custom tag from that tag library is used. If you forget to include the taglib<br />

directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag<br />

from that library as static data and will simply insert the text of the custom tag call into the<br />

response.<br />

<br />

<strong>The</strong> prefix attribute defines the prefix that distinguishes tags defined by a given tag library<br />

from those provided by other tag libraries.<br />

If the tag library is defined with tag files (see “Encapsulating Reusable Content Using Tag Files”<br />

on page 231), you supply the tagdir attribute to identify the location of the files. <strong>The</strong> value of<br />

the attribute must start with /WEB-INF/tags/. A translation error will occur if the value points<br />

to a directory that doesn’t exist or if it is used in conjunction with the uri attribute.<br />

<strong>The</strong> uri attribute refers to a URI that uniquely identifies the tag library descriptor (TLD), a<br />

document that describes the tag library (see “Tag Library Descriptors” on page 245).<br />

Tag library descriptor file names must have the extension .tld. TLD files are stored in the<br />

WEB-INF directory or subdirectory of the WAR file, or in the META-INF directory or subdirectory<br />

of a tag library packaged in a JAR. You can reference a TLD directly or indirectly.<br />

<strong>The</strong> following taglib directive directly references a TLD file name:<br />

<br />

This taglib directive uses a short logical name to indirectly reference the TLD:<br />

<br />

Using CustomTags<br />

<strong>The</strong> iterator example defines and uses a simple iteration tag. <strong>The</strong> JSP pages use a logical name<br />

to reference the TLD.<br />

Chapter 5 • <strong>Java</strong>Server PagesTechnology 171

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!