Contents | Prev | Next


<jsp:invoke>

Evaluates a fragment attribute.

JSP Syntax

<jsp:invoke fragment="fragmentName"	
({var="scopedAttributeName" |	
varReader="scopedAttributeName"}	
[scope="page | request | session | application" ] />) | />

XML Syntax

Same as JSP 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

See Also



Contents | Prev | Next

Copyright © 2004, Sun Microsystems, Inc. All rights reserved.