What if you have tags that work together?

Imagine this scenario...you have a <mine:Menu> tag that builds a custom navigation bar. It needs menu items. So you use a <mine:MenuItem> tag nested within the <mine:Menu> tag, and the menu tag gets ahold (somehow) of the menu items and uses those items to build the navigation bar.

image with no caption

The big question is, how do the tags talk to one another? In other words, how does the Menu tag (the enclosing tag) get the attribute values from the MenuItems (the inner/nested tags)?

Nested tags are used in several places in the JSTL; the <c:choose> tag, with its nested <c:when> and <c:otherwise> tags, is a good example. And you might need to use “cooperating tags” (that’s how the spec says it) in your own custom development as well.

Fortunately, there’s a mechanism for getting info to and from outer and inner tags, regardless of the depth of nesting. That means you can get info from a deeply nested tag out to not just the tag’s immediate enclosing tag, but to any arbitrary tag up the tag nesting hierarchy.

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.