Preface

Comment

Declaration

Expression

Scriptlet

EL Expression

Directives

Attribute Directive

Include Directive

Page Directive

Tag Directive

Taglib Directive

Variable Directive

Standard Actions

<jsp:attribute>

<jsp:body>

<jsp:element>

<jsp:doBody>

<jsp:forward>

<jsp:getProperty>

<jsp:include>

<jsp:invoke>

<jsp:output>

<jsp:plugin>

<jsp:root>

<jsp:setProperty>

<jsp:text>

<jsp:useBean>

<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



宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有