Processing Fragment Attributes

Processing the custom action body is easy and powerful as you can see, but wait, there’s more! The custom action body is actually just a special case of what’s called a JSP fragment in the JSP specification. A JSP fragment is an executable representation of a set of dynamic elements (actions and EL expressions), optionally mixed with template text. When the tag file invokes the fragment, all the dynamic elements in the fragment are executed. Since the elements have access to the current values of all scoped variables, the result typically differs from invocation to invocation, and the tag file can invoke it any number of times (e.g., once or none for a conditional action or multiple times for an iteration action).

In Example 11-4, the <jsp:doBody> action invokes the special fragment representing a custom action element body, but named fragments can also be provided as custom action attributes and be invoked by the tag file. Such fragments are invoked with the <jsp:invoke> action, described in Table 11-2.

Table 11-2. Attributes for <jsp:invoke>

Attribute name

Java type

Dynamic value accepted

Description

fragment
String

No

Mandatory. The name of the fragment to invoke.

var
String

No

Optional. The name of the variable to hold the body evaluation result as a String.

varReader
String

No

Optional. The name of the variable to hold the body evaluation result as a java.io.Reader.

scope
String

No

Optional. The variable scope; one of page ...

Get JavaServer Pages, 3rd 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.