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

9.  Scripting in JSP Pages

10.  JavaServer Faces Technology

JavaServer Faces Technology User Interface

JavaServer Faces Technology Benefits

What Is a JavaServer Faces Application?

A Simple JavaServer Faces Application

Steps in the Development Process

Mapping theFacesServlet Instance

Creating the Pages

Declaring the Tag Libraries

Adding theview andform Tags

Adding a Label Component

Adding an Image

Adding a Text Field

Registering a Validator on a Text Field

Adding a Custom Message

Adding a Button

Displaying Error Messages

Defining Page Navigation

Configuring Error Messages

Developing the Beans

Adding Managed Bean Declarations

User Interface Component Model

User Interface Component Classes

Component Rendering Model

Conversion Model

Event and Listener Model

Validation Model

Navigation Model

Backing Beans

Creating a Backing Bean Class

Configuring a Bean

Using the Unified EL to Reference Backing Beans

The Life Cycle of a JavaServer Faces Page

Restore View Phase

Apply Request Values Phase

Process Validations Phase

Update Model Values Phase

Invoke Application Phase

Render Response Phase

Further Information about 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

User Interface Component Model

JavaServer Faces UI components are configurable, reusable elements that compose the user interfacesof JavaServer Faces applications. A component can be simple, such as a button,or compound, such as a table, which can be composed of multiple components.

JavaServer Faces technology provides a rich, flexible component architecture that includes the following:

  • A set ofUIComponent classes for specifying the state and behavior of UI components

  • A rendering model that defines how to render the components in various ways

  • An event and listener model that defines how to handle component events

  • A conversion model that defines how to register data converters onto a component

  • A validation model that defines how to register validators onto a component

This section briefly describes each of these pieces of the component architecture.

User Interface Component Classes

JavaServer Faces technology provides a set of UI component classes and associated behavioralinterfaces that specify all the UI component functionality, such as holding component state, maintaininga reference to objects, and driving event handling and rendering for a setof standard components.

The component classes are completely extensible, allowing component writers to create their owncustom components. SeeChapter 13, Creating Custom UI Components for an example of a custom image map component.

All JavaServer Faces UI component classes extendUIComponentBase, which defines the default state andbehavior of a UI component. The following set of UI component classes isincluded with JavaServer Faces technology:

  • UIColumn: Represents a single column of data in aUIData component.

  • UICommand: Represents a control that fires actions when activated.

  • UIData: Represents a data binding to a collection of data represented by aDataModel instance.

  • UIForm: Encapsulates a group of controls that submit data to the application. This component is analogous to theform tag in HTML.

  • UIGraphic: Displays an image.

  • UIInput: Takes data input from a user. This class is a subclass ofUIOutput.

  • UIMessage: Displays a localized message.

  • UIMessages: Displays a set of localized messages.

  • UIOutput: Displays data output on a page.

  • UIPanel: Manages the layout of its child components.

  • UIParameter: Represents substitution parameters.

  • UISelectBoolean: Allows a user to set aboolean value on a control by selecting or deselecting it. This class is a subclass ofUIInput.

  • UISelectItem: Represents a single item in a set of items.

  • UISelectItems: Represents an entire set of items.

  • UISelectMany: Allows a user to select multiple items from a group of items. This class is a subclass ofUIInput.

  • UISelectOne: Allows a user to select one item from a group of items. This class is a subclass ofUIInput.

  • UIViewRoot: Represents the root of the component tree.

In addition to extendingUIComponentBase, the component classes also implement one ormorebehavioral interfaces, each of which defines certain behavior for a set of componentswhose classes implement the interface.

These behavioral interfaces are as follows:

  • ActionSource: Indicates that the component can fire an action event. This interface is intended for use with components based on JavaServer Faces technology 1.1_01 and earlier versions.

  • ActionSource2: ExtendsActionSource, and therefore provides the same functionality. However, it allows components to use the unified EL when referencing methods that handle action events.

  • EditableValueHolder: ExtendsValueHolder and specifies additional features for editable components, such as validation and emitting value-change events.

  • NamingContainer: Mandates that each component rooted at this component have a unique ID.

  • StateHolder: Denotes that a component has state that must be saved between requests.

  • ValueHolder: Indicates that the component maintains a local value as well as the option of accessing data in the model tier.

UICommand implementsActionSource2 andStateHolder.UIOutput and component classes that extendUIOutput implementStateHolder andValueHolder.UIInput and component classes that extendUIInput implementEditableValueHolder,StateHolder,andValueHolder.UIComponentBase implementsStateHolder. See theJavaServer Faces Technology 1.2 API Specification for more information on theseinterfaces.

Only component writers will need to use the component classes and behavioral interfacesdirectly. Page authors and application developers will use a standard UI component byincluding a tag that represents it on a JSP page. Most of thecomponents can be rendered in different ways on a page. For example,aUICommand component can be rendered as a button or a hyperlink.

The next section explains how the rendering model works and how page authorschoose how to render the components by selecting the appropriate tags.

Component Rendering Model

The JavaServer Faces component architecture is designed such that the functionality of thecomponents is defined by the component classes, whereas the component rendering can bedefined by a separate renderer. This design has several benefits, including:

  • Component writers can define the behavior of a component once but create multiple renderers, each of which defines a different way to render the component to the same client or to different clients.

  • Page authors and application developers can change the appearance of a component on the page by selecting the tag that represents the appropriate combination of component and renderer.

Arender kit defines how component classes map to component tags that are appropriatefor a particular client. The JavaServer Faces implementation includes a standard HTML renderkit for rendering to an HTML client.

The render kit defines a set ofRenderer classes for each component thatit supports. EachRenderer class defines a different way to render the particularcomponent to the output defined by the render kit. For example, aUISelectOnecomponent has three different renderers. One of them renders the component as aset of radio buttons. Another renders the component as a combo box. Thethird one renders the component as a list box.

Each JSP custom tag defined in the standard HTML render kit is composedof the component functionality (defined in theUIComponent class) and the rendering attributes (definedby theRenderer class). For example, the two tags inTable 10-1 represent aUICommand component rendered in two different ways.

Table 10-1UICommand Tags

Tag

Rendered As

commandButton

Login button.

commandLink

Hyperlink.

The command part of the tags shown inTable 10-1 corresponds to theUICommand class,specifying the functionality, which is to fire an action. The button and hyperlinkparts of the tags each correspond to a separateRenderer class, which defineshow the component appears on the page.

The JavaServer Faces implementation provides a custom tag library for rendering components inHTML. It supports all the component tags listed inTable 10-2. To learn howto use the tags in an example, seeAdding UI Components to a Page Using the HTML Component Tags.

Table 10-2 The UI Component Tags

Tag

Functions

Rendered As

Appearance

column

Represents a columnof data in aUIData component.

A column of data in an HTMLtable

A column in a table

commandButton

Submits a form to the application.

An HTML<input type=type>element, where thetype value can besubmit,reset, orimage

A button

commandLink

Links toanother page or location on a page.

An HTML<a href> element

A hyperlink

dataTable

Represents adata wrapper.

An HTML<table> element

A table that can be updated dynamically

form

Represents aninput form. The inner tags of the form receive the data that willbe submitted with the form.

An HTML<form> element

No appearance

graphicImage

Displays an image.

An HTML<img> element

An image

inputHidden

Allows a page author to include a hidden variable ina page.

An HTML<input type=hidden> element

No appearance

inputSecret

Allows a user to input a stringwithout the actual string appearing in the field.

An HTML<input type=password> element

A text field,which displays a row of characters instead of the actual string entered

inputText

Allows auser to input a string.

An HTML<input type=text> element

A text field

inputTextarea

Allows a userto enter a multiline string.

An HTML<textarea> element

A multi-row text field

message

Displays alocalized message.

An HTML<span> tag if styles are used

A text string

messages

Displays localizedmessages.

A set of HTML<span> tags if styles are used

A text string

outputFormat

Displaysa localized message.

Plain text

Plain text

outputLabel

Displays a nested component as a label fora specified input field.

An HTML<label> element

Plain text

outputLink

Links to another page orlocation on a page without generating an action event.

An HTML<a> element

A hyperlink

outputText

Displaysa line of text.

Plain text

Plain text

panelGrid

Displays a table.

An HTML<table> element with<tr> and<td> elements

A table

panelGroup

Groups a set of components under one parent.

Arow in a table

selectBooleanCheckbox

Allows a user to change the value of aBoolean choice.

An HTML<input type=checkbox> element.

A check box

selectItem

Represents one item in a listof items in aUISelectOne component.

An HTML<option> element

No appearance

selectItems

Represents a listof items in aUISelectOne component.

A list of HTML<option> elements

No appearance

selectManyCheckbox

Displaysa set of check boxes from which the user can select multiple values.

Aset of HTML<input> elements of typecheckbox

A set of check boxes

selectManyListbox

Allowsa user to select multiple items from a set of items, alldisplayed at once.

An HTML<select> element

A list box

selectManyMenu

Allows a user to select multipleitems from a set of items.

An HTML<select> element

A scrollable combo box

selectOneListbox

Allowsa user to select one item from a set of items, alldisplayed at once.

An HTML<select> element

A list box

selectOneMenu

Allows a user to select oneitem from a set of items.

An HTML<select> element

A scrollable combo box

selectOneRadio

Allowsa user to select one item from a set of items.

An HTML<input type=radio> element

A set of radio buttons

Conversion Model

A JavaServer Faces application can optionally associate a component with server-side object data. Thisobject is a JavaBeans component, such as a backing bean. An application getsand sets the object data for a component by calling the appropriateobject properties for that component.

When a component is bound to an object, the application has two viewsof the component’s data:

  • The model view, in which data is represented as data types, such asint orlong.

  • The presentation view, in which data is represented in a manner that can be read or modified by the user. For example, ajava.util.Date might be represented as a text string in the formatmm/dd/yy or as a set of three text strings.

The JavaServer Faces implementation automatically converts component data between these two views whenthe bean property associated with the component is of one of the typessupported by the component’s data. For example, if aUISelectBoolean component is associatedwith a bean property of typejava.lang.Boolean, the JavaServer Faces implementation will automatically convertthe component’s data fromString toBoolean. In addition, some component data mustbe bound to properties of a particular type. For example, aUISelectBooleancomponent must be bound to a property of typeboolean orjava.lang.Boolean.

Sometimes you might want to convert a component’s data to a type otherthan a standard type, or you might want to convert the format ofthe data. To facilitate this, JavaServer Faces technology allows you to register aConverter implementation onUIOutput components and components whose classes subclassUIOutput. If you registertheConverter implementation on a component, theConverter implementation converts the component’sdata between the two views.

You can either use the standard converters supplied with the JavaServer Faces implementationor create your own custom converter.

To create and use a custom converter in your application, three things musthappen:

Event and Listener Model

The JavaServer Faces event and listener model is similar to the JavaBeans eventmodel in that it has strongly typed event classes and listener interfaces thatan application can use to handle events generated by UI components.

AnEvent object identifies the component that generated the event and stores informationabout the event. To be notified of an event, an application must providean implementation of theListener class and must register it on thecomponent that generates the event. When the user activates a component, such asby clicking a button, an event is fired. This causes the JavaServer Facesimplementation to invoke the listener method that processes the event.

JavaServer Faces technology supports three kinds of events: value-change events, action events, anddata-model events.

Anaction event occurs when the user activates a component that implementsActionSource. These componentsinclude buttons and hyperlinks.

Avalue-change event occurs when the user changes the value of a component representedbyUIInput or one of its subclasses. An example is selecting a checkbox, an action that results in the component’s value changing totrue. The componenttypes that can generate these types of events are theUIInput,UISelectOne,UISelectMany, andUISelectBoolean components. Value-change events are fired only if no validation errors weredetected.

Depending on the value of theimmediate property (seeTheimmediate Attribute) of the componentemitting the event, action events can be processed during the invoke application phaseor the apply request values phase, and value-change events can be processed duringthe process validations phase or the apply request values phase.

Adata-model event occurs when a new row of aUIData component is selected.The discussion of data-model events is an advanced topic. It is not coveredin this tutorial but may be discussed in future versions of this tutorial.

There are two ways to cause your application to react to actionevents or value-change events emitted by a standard component:

  • Implement an event listener class to handle the event and register the listener on the component by nesting either avalueChangeListener tag or anactionListener tag inside the component tag.

  • Implement a method of a backing bean to handle the event and refer to the method with a method expression from the appropriate attribute of the component’s tag.

SeeImplementing an Event Listener for information on how to implement an event listener. SeeRegistering Listeners on Componentsfor information on how to register the listener on a component.

SeeWriting a Method to Handle an Action Event andWriting a Method to Handle a Value-Change Event for information on how to implement backing bean methodsthat handle these events.

SeeReferencing a Backing Bean Method for information on how to refer to the backing bean methodfrom the component tag.

When emitting events from custom components, you must implement the appropriateEvent classand manually queue the event on the component in addition to implementing anevent listener class or a backing bean method that handles the event.Handling Events for Custom Componentsexplains how to do this.

Validation Model

JavaServer Faces technology supports a mechanism for validating the local data of editablecomponents (such as text fields). This validation occurs before the corresponding model data isupdated to match the local value.

Like the conversion model, the validation model defines a set of standard classesfor performing common data validation checks. The JavaServer Faces core tag library alsodefines a set of tags that correspond to the standardValidator implementations. SeeTable 11-7 for a list of all the standard validation classes and corresponding tags.

Most of the tags have a set of attributes for configuring thevalidator’s properties, such as the minimum and maximum allowable values for the component’s data.The page author registers the validator on a component by nesting the validator’stag within the component’s tag.

The validation model also allows you to create your own custom validator andcorresponding tag to perform custom validation. The validation model provides two ways toimplement custom validation:

If you are implementing aValidator interface, you must also:

If you are implementing a backing bean method to perform validation, you alsomust reference the validator from the component tag’svalidator attribute. SeeReferencing a Method That Performs Validationfor more information.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp