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 Validators

JavaServer Faces technology provides a set of standard classes and associated tags thatpage authors and application developers can use to validate a component’s data.Table 11-7lists all the standard validator classes and the tags that allow you touse the validators from the page.

Table 11-7 The Validator Classes

Validator Class

Tag

Function

DoubleRangeValidator

validateDoubleRange

Checks whether the local value ofa component is within a certain range. The value must be floating-point orconvertible to floating-point.

LengthValidator

validateLength

Checks whether the length of a component’s local value iswithin a certain range. The value must be ajava.lang.String.

LongRangeValidator

validateLongRange

Checks whether the localvalue of a component is within a certain range. The value must beany numeric type orString that can be converted to along.

All these validator classes implement theValidator interface. Component writers and application developerscan also implement this interface to define their own set of constraints fora component’s value.

Similarly to the standard converters, each of these validators has one or morestandard error messages associated with it. If you have registered one of thesevalidators onto a component on your page, and the validator is not ableto validate the component’s value, the validator’s error message will display on thepage. For example, the error message that displays when the component’s value exceedsthe maximum value allowed byLongRangeValidator is the following:

{1}: Validation Error: Value is greater than allowable maximum of "{0}"

In this case the{1} substitution parameter is replaced by the component’s labelor ID, and the{0} substitution parameter is replaced with the maximum valueallowed by the validator.

See section 2.5.4 of the JavaServer Faces specification for the complete list oferror messages. SeeDisplaying Error Messages with themessage andmessages Tags for information on how to display validation error messageson the page when validation fails.

Validating a Component’s Value

In order to validate a component’s value using a particular validator, you needto register the validator on the component. You have three ways to dothis:

  • Nest the validator’s corresponding tag (shown inTable 11-7) inside the component’s tag.Using theLongRangeValidator describes how to use thevalidateLongRange tag. You can use the other standard tags in the same way.

  • Refer to a method that performs the validation from the component tag’svalidator attribute.

  • Nest avalidator tag inside the component tag and use either the validator tag’svalidatorId attribute or itsbinding attribute to refer to the validator.

SeeReferencing a Method That Performs ValidationReferencing a Method That Performs Validation for more information on using thevalidator attribute.

ThevalidatorId attribute works similarly to theconverterId attribute of theconvertertag, as described inConverting a Component's Value. SeeBinding Converters, Listeners, and Validators to Backing Bean Properties for more information on using thebindingattribute of thevalidator tag.

Keep in mind that validation can be performed only on components that implementEditableValueHolder because these components accept values that can be validated.

Using theLongRangeValidator

The Duke’s Bookstore application uses avalidateLongRange tag on thequantity input field ofthebookshowcart.jsp page:

<h:inputText size="4"     value="#{item.quantity}" >    <f:validateLongRange minimum="1"/></h:inputText><h:message for="quantity"/>

This tag requires that the user enter a number that is atleast 1. Thesize attribute specifies that the number can have nomore than four digits. ThevalidateLongRange tag also has amaximum attribute, with whichyou can set a maximum value of the input.

The attributes of all the standard validator tags accept value expressions. This meansthat the attributes can reference backing bean properties rather than specify literal values.For example, thevalidateLongRange tag in the preceding example can reference a backingbean property calledminimum to get the minimum value acceptable to thevalidator implementation:

<f:validateLongRange minimum="#{ShowCartBean.minimum}" />
PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp