站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > The J2EE 1.4 Tutorial

Types of Tags - The J2EE 1.4 Tutorial

Types of Tags

Simple tags are invoked using XML syntax. They have a start tag and an end tag, and possibly a body:

<tt:tag>
  body
</tt:tag> 

A custom tag with no body is expressed as follows:

<tt:tag /> or <tt:tag></tt:tag> 

Tags with Attributes

A simple tag can have attributes. Attributes customize the behavior of a custom tag just as parameters customize the behavior of a method. There are three types of attributes:

  • Simple attributes
  • Fragment attributes
  • Dynamic attributes

Simple Attributes

Simple attributes are evaluated by the container before being passed to the tag handler. Simple attributes are listed in the start tag and have the syntax attr="value". You can set a simple attribute value from a String constant, or an expression language (EL) expression, or by using a jsp:attribute element (see jsp:attribute Element). The conversion process between the constants and expressions and attribute types follows the rules described for JavaBeans component properties in Setting JavaBeans Component Properties.

The Duke's Bookstore page bookcatalog.jsp calls the catalog tag, which has two attributes. The first attribute, a reference to a book database object, is set by an EL expression. The second attribute, which sets the color of the rows in a table that represents the bookstore catalog, is set with a String constant.

<sc:catalog bookDB ="${bookDB}" color="#cccccc"> 

Fragment Attributes

A JSP fragment is a portion of JSP code passed to a tag handler that can be invoked as many times as needed. You can think of a fragment as a template that is used by a tag handler to produce customized content. Thus, unlike a simple attribute which is evaluated by the container, a fragment attribute is evaluated by a tag handler during tag invocation.

To declare a fragment attribute, you use the fragment attribute of the attribute directive (see Declaring Tag Attributes in Tag Files) or use the fragment subelement of the attribute TLD element (see Declaring Tag Attributes for Tag Handlers). You define the value of a fragment attribute by using a jsp:attribute element. When used to specify a fragment attribute, the body of the jsp:attribute element can contain only static text and standard and custom tags; it cannot contain scripting elements (see Chapter 16).

JSP fragments can be parametrized via expression language (EL) variables in the JSP code that composes the fragment. The EL variables are set by the tag handler, thus allowing the handler to customize the fragment each time it is invoked (see Declaring Tag Variables in Tag Files, and Declaring Tag Variables for Tag Handlers).

The catalog tag discussed earlier accepts two fragments: normalPrice, which is displayed for a product that's full price, and onSale, which is displayed for a product that's on sale.

<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 tag executes the normalPrice fragment, using the values for the price EL variable, if the product is full price. If the product is on sale, the tag executes the onSale fragment using the price and salePrice variables.

Dynamic Attributes

A dynamic attribute is an attribute that is not specified in the definition of the tag. Dynamic attributes are used primarily by tags whose attributes are treated in a uniform manner but whose names are not necessarily known at development time.

For example, this tag accepts an arbitrary number of attributes whose values are colors and outputs a bulleted list of the attributes colored according to the values:

<colored:colored color1="red" color2="yellow" color3="blue"/> 

You can also set the value of dynamic attributes using an EL expression or using the jsp:attribute element.

jsp:attribute Element

The jsp:attribute element allows you to define the value of a tag attribute in the body of an XML element instead of in the value of an XML attribute.

For example, the Duke's Bookstore template page screendefinitions.jsp uses jsp:attribute to use the output of fmt:message to set the value of the value attribute of tt:parameter:

...
<tt:screen id="/bookcatalog">
  <tt:parameter name="title" direct="true">
    <jsp:attribute name="value" >
      <fmt:message key="TitleBookCatalog"/>
    </jsp:attribute>
  </tt:parameter>
  <tt:parameter name="banner" value="/template/banner.jsp"
    direct="false"/>
  <tt:parameter name="body" value="/bookcatalog.jsp"
    direct="false"/>
</tt:screen>
... 

jsp:attribute accepts a name attribute and a trim attribute. The name attribute identifies which tag attribute is being specified. The optional trim attribute determines whether or not whitespace appearing at the beginning and end of the element body should be discarded. By default, the leading and trailing whitespace is discarded. The whitespace is trimmed when the JSP page is translated. If a body contains a custom tag that produces leading or trailing whitespace, that whitespace is preserved regardless of the value of the trim attribute.

An empty body is equivalent to specifying "" as the value of the attribute.

The body of jsp:attribute is restricted according to the type of attribute being specified:

  • For simple attributes that accept an EL expression, the body can be any JSP content.
  • For simple attributes that do not accept an EL expression, the body can contain only static text.
  • For fragment attributes, the body must not contain any scripting elements (see Chapter 16).

Tags with Bodies

A simple tag can contain custom and core tags, HTML text, and tag-dependent body content between the start tag and the end tag.

In the following example, the Duke's Bookstore application page bookshowcart.jsp uses the JSTL c:if tag to print the body if the request contains a parameter named Clear:

<c:if test="${param.Clear}">
  <font color="#ff0000" size="+2"><strong> 
  You just cleared your shopping cart! 
  </strong><br>&nbsp;<br></font>
</c:if> 

jsp:body Element

You can also explicitly specify the body of a simple tag by using the jsp:body element. If one or more attributes are specified with the jsp:attribute element, then jsp:body is the only way to specify the body of the tag. If one or more jsp:attribute elements appear in the body of a tag invocation but you don't include a jsp:body element, the tag has an empty body.

Tags That Define Variables

A simple tag can define an EL variable that can be used within the calling page. In the following example, the iterator tag sets the value of the EL variable departmentName as it iterates through a collection of department names.

<tlt:iterator var="departmentName" type="java.lang.String"
    group="${myorg.departmentNames}">
  <tr>
    <td><a href="list.jsp?deptName=${departmentName}">
      ${departmentName}</a></td>
  </tr>
</tlt:iterator> 

Communication between Tags

Custom tags communicate with each other through shared objects. There are two types of shared objects: public and private.

In the following example, the c:set tag creates a public EL variable called aVariable, which is then reused by anotherTag.

<c:set var="aVariable" value="aValue" />
<tt:anotherTag attr1="${aVariable}" /> 

Nested tags can share private objects. In the next example, an object created by outerTag is available to innerTag. The inner tag retrieves its parent tag and then retrieves an object from the parent. Because the object is not named, the potential for naming conflicts is reduced.

<tt:outerTag>
  <tt:innerTag />
</tt:outerTag> 

The Duke's Bookstore page template.jsp uses a set of cooperating tags that share public and private objects to define the screens of the application. These tags are described in A Template Tag Library.