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

Using the Standard Converters

The JavaServer Faces implementation provides a set ofConverter implementations that you can useto convert component data. For more information on the conceptual details of theconversion model, seeConversion Model.

The standardConverter implementations, located in thejavax.faces.convert package, are as follows:

  • BigDecimalConverter

  • BigIntegerConverter

  • BooleanConverter

  • ByteConverter

  • CharacterConverter

  • DateTimeConverter

  • DoubleConverter

  • FloatConverter

  • IntegerConverter

  • LongConverter

  • NumberConverter

  • ShortConverter

Each of these converters has a standard error message associated with them. Ifyou have registered one of these converters onto a component on your page,and the converter is not able to convert the component’s value, the converter’serror message will display on the page. For example, the error message thatdisplays ifBigIntegerConverter fails to convert a value is:

{0} must be a number consisting of one or more digits

In this case the{0} substitution parameter will be replaced with the nameof the input component on which the converter is registered. See section 2.4.5of the JavaServer Faces specification, version 1.2, for a complete list of errormessages.

Two of the standard converters (DateTimeConverter andNumberConverter) have their own tags,which allow you to configure the format of the component data using thetag attributes.UsingDateTimeConverter discusses usingDateTimeConverter.UsingNumberConverter discusses usingNumberConverter. The following sectionexplains how to convert a component’s value including how to register the otherstandard converters with a component.

Converting a Component’s Value

To use a particular converter to convert a component’s value, you need toregister the converter onto the component. You can register any of the standardconverters on a component in one of four ways:

  • Nest one of the standard converter tags inside the component’s tag. These tags areconvertDateTime andconvertNumber and are described inUsingDateTimeConverter andUsingNumberConverter, respectively.

  • Bind the value of the component to a backing bean property of the same type as the converter.

  • Refer to the converter from the component tag’sconverter attribute.

  • Nest aconverter tag inside of the component tag and use either theconverter tag’sconverterId attribute or itsbinding attribute to refer to the converter.

As an example of the second approach, if you want a component’s datato be converted to anInteger, you can simply bind the component’s valueto a property similar to this:

Integer age = 0;public Integer getAge(){ return age;}public void setAge(Integer age) {this.age = age;}

If the component is not bound to a bean property, you canemploy the third technique by using theconverter attribute on the component tag:

<h:inputText    converter="javax.faces.convert.IntegerConverter" />

This example shows theconverter attribute referring to the fully-qualified class name ofthe converter. Theconverter attribute can also take the ID of thecomponent. If the converter is a custom converter, the ID is defined inthe application configuration resource file (seeApplication Configuration Resource File).

The data corresponding to this exampleinputText tag will be converted to ajava.lang.Integer.Notice that theInteger type is already a supported type of theNumberConverter. If you don’t need to specify any formatting instructions using theconvertNumbertag attributes, and if one of the other converters will suffice, you cansimply reference that converter using the component tag’sconverter attribute.

Finally, you can nest aconverter tag within the component tag and useeither the converter tag’sconverterId attribute or itsbinding attribute to reference theconverter.

TheconverterId attribute must reference the converter’s ID. Again, if the converter isa custom converter, the value ofconverterID must match the ID in the applicationconfiguration resource file; otherwise it must match the ID as defined in theconverter class. Here is an example:

<h:inputText value="#{LoginBean.Age}" />    <f:converter converterId="Integer" /></h:inputText>

Instead of using theconverterId attribute, theconverter tag can use thebinding attribute. Thebinding attribute must resolve to a bean property that accepts andreturns an appropriateConverter instance. SeeBinding Converters, Listeners, and Validators to Backing Bean Properties for more information.

UsingDateTimeConverter

You can convert a component’s data to ajava.util.Date by nesting theconvertDateTime taginside the component tag. TheconvertDateTime tag has several attributes that allow you tospecify the format and type of the data.Table 11-5 lists the attributes.

Here is a simple example of aconvertDateTime tag from thebookreceipt.jsp page:

<h:outputText id= "shipDate" value="#{cashier.shipDate}">    <f:convertDateTime dateStyle="full" /></h:outputText>

When binding theDateTime converter to a component, ensure that the backing beanproperty to which the component is bound is of typejava.util.Date. In thecase of the preceding example,cashier.shipDate must be of typejava.util.Date.

Here is an example of a date and time that the precedingtag can display:

Saturday, Feb 22, 2003

You can also display the same date and time using this tag:

<h:outputText value="#{cashier.shipDate}">    <f:convertDateTime         pattern="EEEEEEEE, MMM dd, yyyy" /></h:outputText>

If you want to display the example date in Spanish, you canuse thelocale attribute:

<h:inputText value="#{cashier.shipDate}">    <f:convertDateTime dateStyle="full"         locale="Locale.SPAIN"        timeStyle="long" type="both" /></h:inputText>

This tag would display

sabado 23 de septiembre de 2006

Please refer to theCustomizing Formats lesson of the Java Tutorial athttp://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html formore information on how to format the output using thepattern attribute oftheconvertDateTime tag.

Table 11-5convertDateTime Tag Attributes

Attribute

Type

Description

binding

DateTimeConverter

Used to bind a converter to a backing bean property

dateStyle

String

Definesthe format, as specified byjava.text.DateFormat, of a date or the date partof adate string. Applied only iftype isdate (or both) andpattern is not defined. Valid values:default,short,medium,long, andfull.If no value is specified,default is used.

locale

String orLocale

Locale whose predefinedstyles for dates and times are used during formatting or parsing. If notspecified, theLocale returned byFacesContext.getLocale will be used.

pattern

String

Custom formatting pattern thatdetermines how the date/time string should be formatted and parsed. If this attributeis specified,dateStyle,timeStyle, andtype attributes are ignored.

timeStyle

String

Defines the format, asspecified byjava.text.DateFormat, of atime or the time part of adatestring. Applied only iftype is time andpattern is not defined. Valid values:default,short,medium,long, andfull. If no value is specified,default isused.

timeZone

String orTimeZone

Time zone in which to interpret any time information inthedate string.

type

String

Specifies whether the string value will contain adate, atime,or both. Valid values aredate,time, or both. If no valueis specified,date is used.

UsingNumberConverter

You can convert a component’s data to ajava.lang.Number by nesting theconvertNumbertag inside the component tag. TheconvertNumber tag has several attributes that allowyou to specify the format and type of the data.Table 11-6 lists theattributes.

Thebookcashier.jsp page of Duke’s Bookstore uses aconvertNumber tag to display thetotal prices of the books in the shopping cart:

<h:outputText value="#{cart.total}" >    <f:convertNumber type="currency"/></h:outputText>

When binding theNumber converter to a component, ensure that the backing beanproperty to which the component is bound is of primitive type or hasa type ofjava.lang.Number. In the case of the preceding example,cart.total isof typejava.lang.Number.

Here is an example of a number this tag can display

$934

This number can also be displayed using this tag:

<h:outputText     value="#{cart.Total}" >    <f:convertNumber pattern="$####" /></h:outputText>

Please refer to theCustomizing Formats lesson of the Java Tutorial athttp://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html formore information on how to format the output using the pattern attribute oftheconvertNumber tag.

Table 11-6convertNumber Attributes

Attribute

Type

Description

binding

NumberConverter

Used to bind a converter to a backing bean property

currencyCode

String

ISO4217 currency code, used only when formatting currencies.

currencySymbol

String

Currency symbol, applied only whenformatting currencies.

groupingUsed

boolean

Specifies whether formatted output contains grouping separators.

integerOnly

boolean

Specifies whether only the integerpart of the value will be parsed.

locale

String orLocale

Locale whose number stylesare used to format or parse data.

maxFractionDigits

int

Maximum number of digits formatted in thefractional part of the output.

maxIntegerDigits

int

Maximum number of digits formatted in the integerpart of the output.

minFractionDigits

int

Minimum number of digits formatted in the fractional partof the output.

minIntegerDigits

int

Minimum number of digits formatted in the integer part ofthe output.

pattern

String

Custom formatting pattern that determines how the number string is formatted andparsed.

type

String

Specifies whether the string value is parsed and formatted as anumber,currency, orpercentage. If not specified,number is used.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp