Movatterモバイル変換


[0]ホーム

URL:


Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

What Is a Custom Tag?

The Example JSP Pages

Types of Tags

Tags with Attributes

Simple Attributes

Fragment Attributes

Dynamic Attributes

Deferred Value

Deferred Method

Dynamic Attribute or Deferred Expression

jsp:attribute Element

Tags with Bodies

jsp:body Element

Tags That Define Variables

Communication between Tags

Encapsulating Reusable Content Using Tag Files

Tag File Location

Tag File Directives

Declaring Tags

body-content Attribute

Declaring Tag Attributes in Tag Files

Declaring Tag Variables in Tag Files

Evaluating Fragments Passed to Tag Files

Custom Tag Examples

Simple Attribute Example

Simple and Fragment Attribute and Variable Example

Dynamic Attribute Example

Tag Library Descriptors

Top-Level Tag Library Descriptor Elements

validator Element

listener Element

Declaring Tag Files

tag-file TLD Element

Unpackaged Tag Files

Packaged Tag Files

Declaring Tag Handlers

body-content Element

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 Basic Tags

Tag Handlers for Tags with Attributes

Defining Attributes in a Tag Handler

Attribute Validation

Setting Dynamic Attributes

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

TagExtraInfo Class

Cooperating Tags

Tag Handler Examples

An Iteration Tag

A Template Tag Library

9.  Scripting in JSP Pages

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

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

18.  Streaming API for XML

19.  SOAP with Attachments API for Java

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

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

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

The Java EE 5 Tutorial

Java Coffee Cup logo
PreviousContentsNext

Types of Tags

Simple tags are invoked using XML syntax. They have a start tagand 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 customtag just as parameters customize the behavior of a method. There are threetypes of attributes:

  • Simple attributes

  • Fragment attributes

  • Dynamic attributes

Simple Attributes

Simple attributes are evaluated by the container before being passed to the taghandler. Simple attributes are listed in the start tag and have the syntaxattr="value". You can set a simple attribute value from aString constant, or anexpression language (EL) expression, or by using ajsp:attribute element (seejsp:attribute Element). Theconversion process between the constants and expressions and attribute types follows the rules describedfor JavaBeans component properties inSetting JavaBeans Component Properties.

The Duke’s Bookstore pagetut-install/javaeetutorial5/examples/web/bookstore3/web/bookcatalog.jsp calls thecatalog tag, which has twoattributes. The first attribute, a reference to a book database object, is setby an EL expression. The second attribute, which sets the color of therows in a table that represents the bookstore catalog, is set with aString constant.

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

AJSP fragment is a portion of JSP code passed to a taghandler that can be invoked as many times as needed. You can thinkof a fragment as a template that is used by a tag handlerto produce customized content. Thus, unlike a simple attribute which is evaluated bythe container, a fragment attribute is evaluated by a tag handler during taginvocation.

To declare a fragment attribute, you use thefragment attribute of theattributedirective (seeDeclaring Tag Attributes in Tag Files) or use thefragment subelement of theattribute TLD element (seeDeclaring Tag Attributes for Tag Handlers). Youdefine the value of a fragment attribute by using ajsp:attribute element. Whenused to specify a fragment attribute, the body of thejsp:attribute elementcan contain only static text and standard and custom tags; itcannot containscripting elements (seeChapter 9, Scripting in JSP Pages).

JSP fragments can be parameterized by means of expression language (EL) variables inthe JSP code that composes the fragment. The EL variables are set bythe tag handler, thus allowing the handler to customize the fragment each timeit is invoked (seeDeclaring Tag Variables in Tag Files, andDeclaring Tag Variables for Tag Handlers).

Thecatalog tag discussed earlier accepts two fragments:normalPrice, which is displayedfor a product that’s full price, andonSale, which is displayed for aproduct 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 thenormalPrice fragment, using the values for theprice ELvariable, if the product is full price. If the product is on sale,the tag executes theonSale fragment using theprice andsalePrice variables.

Dynamic Attributes

Adynamic attribute is an attribute that is not specified in the definition ofthe tag. Dynamic attributes are used primarily by tags whose attributes are treatedin a uniform manner but whose names are not necessarily known at developmenttime.

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

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

You can also set the value of dynamic attributes using an ELexpression or using thejsp:attribute element.

Deferred Value

Adeferred value attribute is one that accepts deferred value expressions, which are described inValue Expressions.

Deferred Method

Adeferred 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, oran EL expression, including deferred expressions.

jsp:attribute Element

Thejsp:attribute element allows you to define the value of a tag attributein thebody of an XML element instead of in the value ofan XML attribute.

For example, the Duke’s Bookstore template pagescreendefinitions.jsp usesjsp:attribute to usethe output offmt:message to set the value of thevalue attributeoftt:parameter:

...<tt:screen>    <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 aname attribute and atrim attribute. Thename attribute identifies whichtag attribute is being specified. The optionaltrim attribute determines whether or notwhite space appearing at the beginning and end of the element body shouldbe discarded. By default, the leading and trailing white space is discarded. Thewhite space is trimmed when the JSP page is translated. If a bodycontains a custom tag that produces leading or trailing white space, that whitespace is preserved regardless of the value of thetrim attribute.

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

The body ofjsp:attribute is restricted according to the type of attribute beingspecified:

  • 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 (seeChapter 9, Scripting in JSP Pages).

Tags with Bodies

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

In the following example, the Duke’s Bookstore application pagetut-install/javaeetutorial5/examples/web/bookstore3/web/bookshowcart.jsp uses theJSTLc:if tag to print the body if the request contains a parameternamedClear:

<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 usingthejsp:body element. If one or more attributes are specified with thejsp:attributeelement, thenjsp:body is the only way to specify the body ofthe tag. If one or morejsp:attribute elements appear in the bodyof a tag invocation but you don’t include ajsp:body element, the taghas an empty body.

Tags That Define Variables

A simple tag can define an EL variable that can be used withinthe calling page. In the following example, theiterator tag sets the valueof the EL variabledepartmentName 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 typesof shared objects: public and private.

In the following example, thec:set tag creates a public EL variable calledaVariable, which is then reused byanotherTag.

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

Nested tags can share private objects. In the next example, an object createdbyouterTag is available toinnerTag. The inner tag retrieves its parent tagand then retrieves an object from the parent. Because the object is notnamed, the potential for naming conflicts is reduced.

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

The Duke’s Bookstore pagetut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp uses a set of cooperating tags thatshare public and private objects to define the screens of the application. Thesetags are described inA Template Tag Library.

PreviousContentsNext

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices


[8]ページ先頭

©2009-2025 Movatter.jp