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.

Communication betweenTags<br />

Custom tags communicate with each other through shared objects. <strong>The</strong>re are two types of<br />

shared objects: public and private.<br />

In the following example, the c:set tag creates a public EL variable called aVariable, which is<br />

then reused by anotherTag.<br />

<br />

<br />

Nested tags can share private objects. In the next example, an object created by outerTag is<br />

available to innerTag. <strong>The</strong> inner tag retrieves its parent tag and then retrieves an object from<br />

the parent. Because the object is not named, the potential for naming conflicts is reduced.<br />

<br />

<br />

<br />

<strong>The</strong> Duke’s Bookstore page<br />

tut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp uses<br />

a set of cooperating tags that share public and private objects to define the screens of the<br />

application. <strong>The</strong>se tags are described in “A Template Tag Library” on page 264.<br />

Encapsulating Reusable Content UsingTag Files<br />

A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag<br />

files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a<br />

servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.<br />

<strong>The</strong> recommended file extension for a tag file is .tag. As is the case with JSP files, the tag can be<br />

composed of a top file that includes other files that contain either a complete tag or a fragment<br />

of a tag file. Just as the recommended extension for a fragment of a JSP file is .jspf, the<br />

recommended extension for a fragment of a tag file is .tagf.<br />

<strong>The</strong> following version of the Hello, World application introduced in Chapter 3, “Getting Started<br />

with Web Applications,” uses a tag to generate the response. <strong>The</strong> response tag, which accepts<br />

two attributes (a greeting string and a name) is encapsulated in response.tag:<br />

<br />

<br />

${greeting}, ${name}!<br />

Encapsulating Reusable Content UsingTag Files<br />

<strong>The</strong> highlighted line in the greeting.jsp page invokes the response tag if the length of the<br />

username request parameter is greater than 0:<br />

Chapter 8 • CustomTags in JSP Pages 231

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

Saved successfully!

Ooh no, something went wrong!