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

11.  Using JavaServer Faces Technology in JSP Pages

The Example JavaServer Faces Application

Setting Up a Page

Using the Core Tags

Adding UI Components to a Page Using the HTML Component Tags

UI Component Tag Attributes

Theid Attribute

Theimmediate Attribute

Therendered Attribute

Thestyle andstyleClass Attributes

Thevalue andbinding Attributes

Adding a Form Component

Using Text Components

Rendering a Text Field with theinputText Tag

Rendering a Label with theoutputLabel Tag

Rendering a Hyperlink with theoutputLink Tag

Displaying a Formatted Message with theoutputFormat Tag

Rendering a Password Field with theinputSecret Tag

Using Command Components for Performing Actions and Navigation

Rendering a Button with thecommandButton Tag

Rendering a Hyperlink with thecommandLink Tag

Using Data-Bound Table Components

Adding Graphics and Images with thegraphicImage Tag

Laying Out Components with theUIPanel Component

Rendering Components for Selecting One Value

Displaying a Check Box Using theselectBooleanCheckbox Tag

Displaying a Menu Using theselectOneMenu Tag

Rendering Components for Selecting Multiple Values

TheUISelectItem,UISelectItems, andUISelectItemGroup Components

Using theselectItems Tag

Using theselectItem Tag

Displaying Error Messages with themessage andmessages Tags

Using Localized Data

Loading a Resource Bundle

Referencing Localized Static Data

Referencing Error Messages

Using the Standard Converters

Converting a Component's Value

UsingDateTimeConverter

UsingNumberConverter

Registering Listeners on Components

Registering a Value-Change Listener on a Component

Registering an Action Listener on a Component

Using the Standard Validators

Validating a Component's Value

Using theLongRangeValidator

Binding Component Values and Instances to External Data Sources

Binding a Component Value to a Property

Binding a Component Value to an Implicit Object

Binding a Component Instance to a Bean Property

Binding Converters, Listeners, and Validators to Backing Bean Properties

Referencing a Backing Bean Method

Referencing a Method That Performs Navigation

Referencing a Method That Handles an Action Event

Referencing a Method That Performs Validation

Referencing a Method That Handles a Value-change Event

Using Custom Objects

Using a Custom Converter

Using a Custom Validator

Using a Custom Component

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

Binding Component Values and Instances to External Data Sources

As explained inBacking Beans, a component tag can wire its component’s data toa back-end data object by doing one of the following:

  • Binding its component’s value to a bean property or other external data source

  • Binding its component’s instance to a bean property

A component tag’svalue attribute uses a value expression to bind the component’svalue to an external data source, such as a bean property. A componenttag’sbinding attribute uses a value expression to bind a component instance toa bean property.

When a component instance is bound to a backing bean property, theproperty holds the component’s local value. Conversely, when a component’s value is bound toa backing bean property, the property holds the value stored in the backingbean. This value is updated with the local value during the update modelvalues phase of the life cycle. There are advantages to both of thesetechniques.

Binding a component instance to a bean property has these advantages:

  • The backing bean can programmatically modify component attributes.

  • The backing bean can instantiate components rather than let the page author do so.

Binding a component’s value to a bean property has these advantages:

  • The page author has more control over the component attributes.

  • The backing bean has no dependencies on the JavaServer Faces API (such as the UI component classes), allowing for greater separation of the presentation layer from the model layer.

  • The JavaServer Faces implementation can perform conversions on the data based on the type of the bean property without the developer needing to apply a converter.

In most situations, you will bind a component’s value rather than its instanceto a bean property. You’ll need to use a component binding only whenyou need to change one of the component’s attributes dynamically. For example, ifan application renders a component only under certain conditions, it can set thecomponent’srendered property accordingly by accessing the property to which the component isbound.

When referencing the property using the component tag’svalue attribute, you needto use the proper syntax. For example, suppose a backing bean calledMyBeanhas thisint property:

int currentOption = null;int getCurrentOption(){...}void setCurrentOption(int option){...}

The value attribute that references this property must have this value-binding expression:

#{MyBean.currentOption}

In addition to binding a component’s value to a bean property, thevalueattribute can specify a literal value or can map the component’s data toany primitive (such asint), structure (such as an array), or collection (suchas a list), independent of a JavaBeans component.Table 11-8 lists some example value-binding expressionsthat you can use with thevalue attribute.

Table 11-8 Example Value-binding Expressions

Value

Expression

A Boolean

cart.numberOfItems > 0

A property initialized froma contextinit parameter

initParam.quantity

A bean property

CashierBean.name

Value in an array

books[3]

Value in a collection

books["fiction"]

Propertyof an object in an array of objects

books[3].price

The next two sections explain in more detail how to use thevalue attribute to bind a component’s value to a bean property or otherexternal data sources and how to use thebinding attribute to bind a componentinstance to a bean property.

Binding a Component Value to a Property

To bind a component’s value to a bean property, you specify the nameof the bean and the property using thevalue attribute. As explained inBacking Beans, the value expression of the component tag’svalue attribute must match the correspondingmanaged bean declaration in the application configuration resource file.

This means that the name of the bean in the value expressionmust match themanaged-bean-name element of the managed bean declaration up to the firstperiod (.) in the expression. Similarly, the part of the value expression afterthe period must match the name specified in the correspondingproperty-name element in theapplication configuration resource file.

For example, consider this managed bean configuration, which configures theImageArea bean correspondingto the North America part of the image map on thechooselocale.jsp pageof the Duke’s Bookstore application:

<managed-bean>    <managed-bean-name> NA </managed-bean-name>    <managed-bean-class> model.ImageArea </managed-bean-class>    <managed-bean-scope> application </managed-bean-scope>    <managed-property>        <property-name>shape</property-name>        <value>poly</value>    </managed-property>    <managed-property>        <property-name>alt</property-name>        <value>NAmerica</value>    </managed-property>    ...</managed-bean>

This example configures a bean calledNA, which has several properties, one ofwhich is calledshape.

Although thearea tags on thechooselocale.jsp page do not bind toanImageArea property (they bind to the bean itself), to do this, yourefer to the property using a value expression from thevalue attribute ofthe component’s tag:

<h:outputText value="#{NA.shape}" />

Much of the time you will not include definitions for a managedbean’s properties when configuring it. You need to define a property and itsvalue only when you want the property to be initialized with a valuewhen the bean is initialized.

If a component tag’svalue attribute must refer to a property that isnot initialized in themanaged-bean configuration, the part of the value-binding expression afterthe period must match the property name as it is defined in thebacking bean.

SeeApplication Configuration Resource File for information on how to configure beans in the application configurationresource file.

Writing Bean Properties explains in more detail how to write the backing bean properties foreach of the component types.

Binding a Component Value to an Implicit Object

One external data source that avalue attribute can refer to is animplicit object.

Thebookreceipt.jsp page of the Duke’s Bookstore application includes a reference to animplicit object from a parameter substitution tag:

<h:outputFormat title="thanks" value="#{bundle.ThankYouParam}">    <f:param value="#{sessionScope.name}"/></h:outputFormat>

This tag gets the name of the customer from the session scopeand inserts it into the parameterized message at the keyThankYouParam from the resourcebundle. For example, if the name of the customer is Gwen Canigetit, thistag will render:

Thank you, Gwen Canigetit, for purchasing your books from us.

Thename tag on thebookcashier.jsp page has theNameChanged listener implementation registered onit. This listener saves the customer’s name in the session scope when thebookcashier.jsp page is submitted. SeeImplementing Value-Change Listeners for more information on how this listenerworks. SeeRegistering a Value-Change Listener on a Component to learn how the listener is registered on the tag.

Retrieving values from otherimplicit objects is done in a similar wayto the example shown in this section.Table 11-9 lists the implicit objects that avalue attribute can refer to. All of the implicit objects except for thescope objects are read-only and therefore should not be used as a valuefor aUIInput component.

Table 11-9 Implicit Objects

Implicit Object

What It Is

applicationScope

AMap of the application scopeattribute values, keyed by attribute name

cookie

AMap of the cookie values for thecurrent request, keyed by cookie name

facesContext

TheFacesContext instance for the current request

header

AMapof HTTP header values for the current request, keyed by header name

headerValues

AMapofString arrays containing all the header values for HTTP headers in thecurrent request, keyed by header name

initParam

AMap of the context initialization parameters for thisweb application

param

AMap of the request parameters for this request, keyed by parametername

paramValues

AMap ofString arrays containing all the parameter values for request parametersin the current request, keyed by parameter name

requestScope

AMap of the request attributesfor this request, keyed by attribute name

sessionScope

AMap of the session attributes forthis request, keyed by attribute name

view

The rootUIComponent in the current component tree storedin theFacesRequest for this request

Binding a Component Instance to a Bean Property

A component instance can be bound to a bean property using avalue expression with thebinding attribute of the component’s tag. You usually bind acomponent instance rather than its value to a bean property if the beanmust dynamically change the component’s attributes.

Here are two tags from thebookcashier.jsp page that bind components to beanproperties:

<h:selectBooleanCheckbox        rendered="false"    binding="#{cashier.specialOffer}" /><h:outputLabel for="fanClub"    rendered="false"            binding="#{cashier.specialOfferText}"  >    <h:outputText        value="#{bundle.DukeFanClub}"    /></h:outputLabel>

TheselectBooleanCheckbox tag renders a check box and binds thefanClubUISelectBoolean component tothespecialOffer property ofCashierBean. TheoutputLabel tag binds the component representing the checkbox’s label to thespecialOfferText property ofCashierBean. If the application’s locale isEnglish, theoutputLabel tag renders:

I’d like to join the Duke Fan Club, free with my purchase of over $100

Therendered attributes of both tags are set tofalse, which prevents thecheck box and its label from being rendered. If the customer orders morethan $100 (or 100 euros) worth of books and clicks theSubmit button, thesubmit method ofCashierBean sets both components’rendered properties totrue, causing the checkbox and its label to be rendered.

These tags use component bindings rather than value bindings because the backing beanmust dynamically set the values of the components’rendered properties.

If the tags were to use value bindings instead of component bindings, thebacking bean would not have direct access to the components, and would thereforerequire additional code to access the components from theFacesContext instance tochange the components’rendered properties.

Writing Properties Bound to Component Instances explains how to write the bean properties bound to the example componentsand also discusses how thesubmit method sets therendered properties of the components.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp