Types of Tags - The Java EE 5 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
Encapsulating Reusable Content Using Tag Files
Declaring Tag Attributes in Tag Files
Declaring Tag Variables in Tag Files
Evaluating Fragments Passed to Tag Files
Simple and Fragment Attribute and Variable Example
Top-Level Tag Library Descriptor Elements
Declaring Tag Attributes for Tag Handlers
Declaring Tag Variables for Tag Handlers
Programming Simple Tag Handlers
Including Tag Handlers in Web Applications
How Is a Simple Tag Handler Invoked?
Tag Handlers for Tags with Attributes
Defining Attributes in a Tag Handler
Setting Deferred Value Attributes and Deferred Method Attributes
Tag Handlers for Tags with Bodies
Tag Handler Does Not Manipulate the Body
Tag Handlers for Tags That Define Variables
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
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
A custom tag with no body is expressed as follows:
<tt:tag /> or 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 syntaxattr="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 tut-install/javaeetutorial5/examples/web/bookstore3/web/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 aString 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 attributedirective (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 9, Scripting in JSP Pages).
JSP fragments can be parameterized by means of 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 name="onSale">
<fmt:formatNumber value="${price}"
type="currency"/>
<fmt:formatNumber value="${salePrice}"
type="currency"/>
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.
Deferred Value
A deferred value attribute is one that accepts deferred value expressions, which are described inValue Expressions.
Deferred Method
A deferred method attribute is one that accepts deferred method expressions, which are described inMethod Expressions.
Dynamic Attribute or Deferred Expression
This kind of attribute can accept a String literal, a scriptlet expression, or an EL expression, including deferred expressions.
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"/> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookcatalog.jsp" direct="false"/> ...
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 white space appearing at the beginning and end of the element body should be discarded. By default, the leading and trailing white space is discarded. The white space is trimmed when the JSP page is translated. If a body contains a custom tag that produces leading or trailing white space, that white space 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 9, Scripting in JSP Pages).
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 tut-install/javaeetutorial5/examples/web/bookstore3/web/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}">
You just cleared your shopping cart!
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:attributeelement, 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}">
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 calledaVariable, 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 />
The Duke’s Bookstore page tut-install/javaeetutorial5/examples/web/bookstore3/web/template/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.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices