<jsp:invoke>
Evaluates a fragment attribute.
JSP Syntax
<jsp:invoke fragment="fragmentName"
({var="scopedAttributeName" |
varReader="scopedAttributeName"}
[scope="page | request | session | application" ] />) | />
XML Syntax
Examples
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>
Description
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.
Attributes
fragment="fragmentName"var="scopedAttributeName"- The name of a scoped attribute to store the result of the fragment invocation in, as a
java.lang.Stringobject. A translation error must occur if bothvarandvarReaderare specified. If neithervarnorvarReaderare specified, the result of the fragment goes directly to theJspWriter.
- The name of a scoped attribute to store the result of the fragment invocation in, as a
varReader="scopedAttributeName"- The name of a scoped attribute to store the result of the fragment invocation in, as a
java.io.Readerobject. A translation error must occur if bothvarandvarReaderare specified. If neithervarnorvarReaderis specified, the result of the fragment invocation goes directly to theJspWriter.
- The name of a scoped attribute to store the result of the fragment invocation in, as a
scope="page | request | session | application"- The scope in which to store the resulting variable. A translation error must result if the value is not one of
page,request,session, orapplication. A translation error will result if this attribute appears without specifying either thevarorvarReaderattribute 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 anIllegalStateExceptionat runtime if scope issessionand the calling page does not participate in a session. Defaults topage.var="scopedAttributeName".
- The scope in which to store the resulting variable. A translation error must result if the value is not one of
