Evaluates a fragment attribute.
<jsp:invoke fragment="fragmentName" ({var="scopedAttributeName" | varReader="scopedAttributeName"} [scope="page | request | session | application" ] />) | />
The following tag file represents a tag that renders the catalog of a book database as an HTML table. The tag file declares that it sets variables, which are used by two fragment attributes. Before the tag invokes the fragment attributes using the jsp:invoke
element, the Web container passes values for the variables back to the calling page.
<%@ attribute name="bookDB" required="true" type="database.BookDB" %> <%@ attribute name="color" required="true" %> <%@ attribute name="normalPrice" fragment="true" %> <%@ attribute name="onSale" fragment="true" %> <%@ variable name-given="price" %> <%@ variable name-given="salePrice" %> <center> <table> <c:forEach var="book" begin="0" items="${bookDB.books}"> ... <c:set var="salePrice" value="${book.price * .85}" /> <c:set var="price" value="${book.price}" /> <c:choose> <c:when test="${book.onSale}" > <jsp:invoke fragment="onSale" /> </c:when> <c:otherwise> <jsp:invoke fragment="normalPrice"/> </c:otherwise> </c:choose> ... </table> </center>
The following page invokes the tag. The formatting of the book price is determined by two fragment attributes--normalPrice
and onSale
--that are conditionally invoked by the tag according to data retrieved from the book database.
<sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike> <fmt:formatNumber value="${price}" type="currency"/> </strike><br/> <font color="red"> <fmt:formatNumber value="${salePrice}" type="currency"/> </font> </jsp:attribute> </sc:catalog>
The jsp:invoke
standard action takes the name of an attribute that is a fragment, and invokes the fragment, sending the output of the result to the JspWriter
, or to a scoped attribute that can be examined and manipulated. If the fragment identified by the given name is null, jsp:invoke
will behave as though a fragment was passed in that produces no output.
The most basic usage of this standard action will invoke a fragment with the given name with no parameters. It is also possible to invoke the fragment and send the results to a scoped attribute for further examination and manipulation. This can be accomplished by specifying the var
or varReader
attribute in the action. If var is specified, the container stores the result in an EL variable of type String
with the name specified by var
. If varReader
is specified, the container stores the result in an EL variable of type java.io.Reader
, with the name specified by varReader
. The Reader
object can then be passed to a custom tag for further processing. A translation error occurs if both var
and varReader
are specified.
fragment="
fragmentName"
var="
scopedAttributeName"
java.lang.String
object. A translation error must occur if both var
and varReader
are specified. If neither var
nor varReader
are specified, the result of the fragment goes directly to the JspWriter
.
varReader="
scopedAttributeName"
java.io.Reader
object. A translation error must occur if both var
and varReader
are specified. If neither var
nor varReader
is specified, the result of the fragment invocation goes directly to the JspWriter
.
scope="
page | request | session | application"
page
, request
, session
, or application
. A translation error will result if this attribute appears without specifying either the var
or varReader
attribute as well. Note that a value of session should be used with caution since not all calling pages may be participating in a session. A container must throw an IllegalStateException
at runtime if scope is session
and the calling page does not participate in a session. Defaults to page.var="
scopedAttributeName"
.