Mastering JSF

download Mastering JSF

If you can't read please download the document

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Mastering JSF

Mastering JavaServer FacesBill Dudney Jonathan Lehr Bill Willis LeRoy Mattingly

Mastering JavaServer FacesBill Dudney Jonathan Lehr Bill Willis LeRoy Mattingly

Wiley Publishing, Inc. Vice President and Executive Group Publisher: Richard Swadley Vice President and Executive Publisher: Robert Ipsen Vice President and Publisher: Joseph B. Wikert Executive Editorial Director: Mary Bednarek Editorial Manager: Kathryn A. Malm Executive Editor: Robert Elliott Managing Production Editor: Fred Bernardi Development Editor: Eileen Bien Calabro, Sharon Nash Production Editor: Felicia Robinson Media Development Specialist: Greg Stafford Text Design & Composition: Wiley Composition Services Copyright 2004 by Wiley Publishing Inc., Indianapolis, Indiana. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, E-mail: [email protected] Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley Publishing logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. JavaServer is a trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Cataloging-in-Publication Data is available from the publisher. ISBN: 0-471-46207-1 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1

For Andrew, Isaac, Anna, Sophia, and Isabelmay you dream big dreams and have the courage to pursue them. BD To my little Sunshine, who makes me see life in a different light. BW For my wife, Kathryn. JL


Dedication Acknowledgments About the Authors Foreword Introduction The Genius of JSF JSF Makes Things Easy What Is in the Book Layout of the Book Summary

iii xi xiii xv xvii xvii xxviii xix xxi xxiii

Part 1Chapter 1

Architecture of JSFJSF Patterns and Architecture Overview MVC and User InterfacesMVC Pattern Model 2 - MVC for the Web

13 3 44 8

Implementing MVCSwing Collapsed MVC Strategy Struts Model-2 MVC Strategy JSF Component-Based Model-2 MVC Strategy

910 11 18

Composite ComponentsComposite Pattern Swing Components and Containers Struts and Tiles JSF Component Trees

2627 29 33 36



Contents Component EventsObserver Pattern Swing Event Listeners Struts Application Events JSF Events

3838 40 43 44

Summary Chapter 2 Elements of JSF OverviewWeb Development

46 47 4848

UI ComponentsStandard UI Components Identifiers UI Component Trees Tree Manipulation and Navigation Facets Generic Attributes Data Models

4950 53 54 55 56 60 61

ValidationValidators Validator Registration Standard Validators

6262 67 68

ConversionConverters Converter Registration Standard Converters

6969 72 72

Events and ListenersUI Events Listeners Phase Identifiers Event Queuing and Broadcasting

7373 74 75 76

RenderingDelegated Rendering Render Kits Registering Renderers Standard RenderKits

7676 78 78 79

Summary Chapter 3 JSF Request-Processing Life Cycle OverviewFaces Request Generates Faces Response Non-Faces Request Generates Faces Response Faces Request Generates Non-Faces Response Non-Faces Request Generates Non-Faces Response

80 81 8183 83 84 85

Faces ContextAccessing Context Component Tree

8586 86

ContentsExternal Context Flow Control Localization Message Queue 87 88 88 90


Event Queues Standard JSF Request-Processing Life CycleRestore View Apply Request Values Process Validations Update Model Values Invoke Application Render Response

93 9596 96 98 100 101 101



Part 2Chapter 4

Elements of JSFJSF Configuration Overview Getting Started Introduction to Sample Application A Word about Web ApplicationsWeb Application Structure Web Application Deployment Descriptor

105107 107 108 108 110110 113

Configuring a Web Application for JSFFacesServlet Mapping JSF Application Configuration Files Necessary Classes and Resources

114115 116 117

Configuring a JSF ApplicationConfiguration File Format


Tool Support for JSF Configurations Building the Sample JSF Application Summary Chapter 5 JSP Integration in JSF Overview Quick JSF/JSP Integration Example JSP Integration and the Render Response PhaseNew JSF Requests Subsequent JSF Requests

152 153 154 155 155 156 160160 166

JSF JSP Integration Example Summary Chapter 6 UI Components Overview Using ComponentsThe View Subviews

167 173 175 176 177184 186


ContentsValue Binding Converting between Model Properties and UI Strings Processing Submitted Form Values 189 198 201

Validators Configuring Event Listeners Working with TablesUsing Facets A dataTable Example Using a panelGrid

202 203 208209 210 215

Working with Select Lists Summary Chapter 7 Navigation, Actions, and Listeners Overview Actions and NavigationImplementing Application Actions Specifying Navigation Rules

218 222 223 223 224227 229

Working with FormsComplex Forms Complex Navigation

230235 252

Events and ListenersImplementing an ActionListener Method Implementing the ActionListener Interface Implementing a ValueChangeListener Method Implementing the ValueChangeListener Interface

261262 265 268 271

Summary Chapter 8 Validation and Conversion Overview Using ConvertersSetting the converter Attribute in Input Tags Standard Converters

274 275 275 277280 283

Using Custom Converters for ValidationDisplaying Error Messages Registering the Converter Using Converters to Validate Custom Value Types Converter Registration by Type

285289 289 290 293

Using ValidatorsAdding Validator Bindings in JSP Validator Methods Displaying Error Messages

296297 299 300

Creating Custom ValidatorsAdding Attributes to Custom Validators Creating a Custom Validator Tag

300302 305

Conversion and Validation Processing Summary

309 312



Part 3Chapter 9

Applying JSFBuilding JSF Applications Overview Architectural Overview JSF in the Architecture Connecting View, Controller, and Model Objects iBank Bill Payment ApplicationLogin Screen Account Summary Review Historical Payments

313315 315 316 318 319 320321 322 323

Logging into iBankConfiguration The Two-Tiered Model for Login Three-Tiered Model for Login

324328 330 332

Welcome to iBank Making an iBank Payment Reviewing iBank Payments Summary Chapter 10 Custom JSF Components Overview What Is a Custom Component?Aggregate Component New HTML Generator New Client Type

335 337 339 344 345 345 346346 348 349

Motivation for Building a Custom Component Aspects of Custom ComponentsRestore View Apply Request Values Process Validations Update Model Values Invoke Application Render Response

349 351352 353 354 356 357 357

Custom Component ExampleUIScroller Component Code UIScroller Renderer Code UIScroller JSP Tag Code

358359 367 375

Summary Chapter 11 Converting a Struts Application to JSF Overview Defining the Example Problem: The SimpleBloggerUse Cases Screen Design State Diagram Domain Classes

382 383 384 384384 387 388 389


Contents The Struts Version of the SimpleBloggerDomain Classes Struts Configuration Files JSP Pages Form Classes Action Classes

390390 390 392 395 398

The JSF Version of the SimpleBloggerSet Up the Environment Copy the Business Model Objects Convert the Struts ActionForm Classes to Managed Beans Convert the JSP Pages Build the Navigation Model Add the Action Handlers

401402 405 405 408 420 421

Summary Appendix Whats on the Web Site System RequirementsFor Linux For Macintosh

428 431 431432 432

References Index

433 435