Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


JavaServer Faces JSF in Action, Manuais, Projetos, Pesquisas de Informática

Livro sobre java

Tipologia: Manuais, Projetos, Pesquisas

2014

Compartilhado em 04/04/2014

gustavo-dias-11
gustavo-dias-11 🇧🇷

4.7

(7)

46 documentos

1 / 1073

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Kito D. Mann
Foreword by Ed Burns
MANNING
JAVAS
ERVER
FACES
INACTION
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe JavaServer Faces JSF in Action e outras Manuais, Projetos, Pesquisas em PDF para Informática, somente na Docsity!

Kito D. Mann

Foreword by Ed Burns

M A N N I N G

JAVASERVER

FACES

INACTION

For online information and ordering of this and other Manning books, please go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661- Greenwich, CT 06830 email: [email protected]

©2005 by Manning Publications Co. All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.

Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end.

All screens shots of Oracle JDeveloper in appendix B are reproduced with the permission of Oracle Corp. Copyright Oracle Corp, 2004.

All screens shots of WebSphere Studio in appendix B are reproduced with the permission of IBM Corp. Copyright IBM Corp, 2004.

Manning Publications Co. Copyeditor: Liz Welch 209 Bruce Park Avenue Typesetter: Denis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes

ISBN 1-932394-11-

Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 08 07 06 05 04

To my beautiful wife and best friend, Tracey.

This book would not exist without you, and I’m eternally grateful

for the positive influence you’ve had on my life,

always pushing me to be the best I can be.

vii

PART 1 E XPLORING J AVAS ERVER FACES .................................. 1

1 ■^ Introducing JavaServer Faces 3 2 ■^ JSF fundamentals 38 3 ■^ Warming up: getting around JSF 88 4 ■^ Getting started with the standard components 137 5 ■^ Using the input and data table components 185 6 ■^ Internationalization, validators, and converters 234

PART 2 B UILDING USER INTERFACES ..................................... 275

7 ■^ Introducing ProjectTrack 277 8 ■^ Developing a user interface without Java code: the Login page 287 9 ■^ Developing a user interface without Java code: the other pages 316 10 ■^ Integrating application functionality 354

brief contents

viii BRIEF CONTENTS

PART 3 D EVELOPING APPLICATION LOGIC ............................ 407

11 ■^ The JSF environment 409 12 ■^ Building an application: design issues and foundation classes 456 13 ■^ Building an application: backing beans, security, and internationalization 499 14 ■^ Integrating JSF with Struts and existing applications 568

PART 4 W RITING CUSTOM COMPONENTS, RENDERERS,

VALIDATORS , AND CONVERTERS .............................. 603

15 ■^ The JSF environment: a component developer’s perspective 605

PART 5 W RITING CUSTOM COMPONENTS, RENDERERS,

VALIDATORS , AND CONVERTERS : EXAMPLES ........... 703

16 ■^ UIInputDate: a simple input component 705 17 ■^ RolloverButton renderer: a renderer with JavaScript support 727 18 ■^ UIHeadlineViewer: a composite, data-aware component 756 19 ■^ UINavigator: a model-driven toolbar component 794 20 ■^ Validator and converter examples 839

O NLINE E XTENSION

The five chapters in part 5 (plus four additional appendixes) are not included in the print edition. They are available for download in PDF format from the book’s web page to owners of this book. For free access to the online extension please go to www. manning.com/mann.

x CONTENTS

1.5 Hello, world! 22 Dissecting hello.jsp 24 ■^ Dissecting goodbye.jsp 31 Examining the HelloBean class 32 ■^ Configuration with faces-config.xml 34 ■^ Configuration with web.xml 36 1.6 Summary 37

2

JSF fundamentals 38 2.1 The key pieces of the pie 39 User interface components 41 ■^ Renderers 43 Validators 44 ■^ Backing beans 45 ■^ Converters 48 Events and listeners 49 ■^ Messages 55 ■^ Navigation 56 2.2 The Request Processing Lifecycle 57 Phase 1: Restore View 61 ■^ Phase 2: Apply Request Values 63 Phase 3: Process Validations 65 ■^ Phase 4: Update Model Values 66 ■^ Phase 5: Invoke Application 66 ■^ Phase 6: Render Response 68 2.3 Understanding component and client identifiers 69 Naming containers 72 ■^ Referencing identifiers 73 2.4 Exploring the JSF expression language 76 Understanding scoped variables 80 ■^ Using implicit variables 81 ■^ Using the EL with components 83 2.5 Summary 86

3

Warming up: getting around JSF 88 3.1 Setting up your JSF environment 89 Basic requirements 89 ■^ Choosing a JSF implementation 89 Directory structure 90 ■^ Configuration 92 3.2 The role of JSP 102 Using JSP includes 103 ■^ Using JSF with JSTL and other JSP custom tags 104 3.3 Creating and initializing beans 110 Declaring managed beans 113 ■^ Declaring Lists and Maps as managed beans 123 ■^ Setting values with value-binding expressions 125 3.4 Navigating the sea of pages 129 3.5 Summary 136

CONTENTS xi

4

Getting started with the standard components 137 4.1 It’s all in the components 138 Using HTML attributes 142 ■^ Understanding facets 143 The power of tools 145 ■^ The render kit behind the scenes 148 4.2 Common component properties 148 4.3 Controlling the page with UIViewRoot 149 4.4 Setting component parameters with UIParameter 151 4.5 Displaying data with the Output components 153 Displaying ordinary text with HtmlOutputText 153 ■^ Using UIOutput with the <f:verbatim> tag 155 ■^ Creating input labels with HtmlOutputLabel 158 ■^ Using HtmlOutputFormat for parameterized text 160 ■^ Displaying hyperlinks with HtmlOutputLink 165 4.6 Displaying images with HtmlGraphicImage 167 4.7 Displaying component messages with HtmlMessage 169 4.8 Displaying application messages with HtmlMessages 172 4.9 Grouping and layout with the Panel components 176 Grouping components with HtmlPanelGroup 176 Creating tables with HtmlPanelGrid 178 4.10 Summary 184

5

Using the input and data table components 185 5.1 Registering event listeners 186 Declaring value-change listeners 187 Declaring action listeners 187 5.2 Common component properties 189 5.3 Handling forms with HtmlForm 190 5.4 Handling basic user input 192 Declaring basic text fields with HtmlInputText 193 ■^ Using HtmlInputTextarea for memo fields 194 ■^ Displaying password fields with HtmlInputSecret 195 ■^ Declaring hidden fields with HtmlInputHidden 197

CONTENTS xiii

P ART 2 B UILDING USER INTERFACES ............................... 275

7

Introducing ProjectTrack 277 7.1 Requirements 278 7.2 The conceptual model 281 7.3 User interface 283 7.4 Development team 284 7.5 Summary 286

8

Developing a user interface without Java code: the Login page 287 8.1 Getting started 289 Setting up web.xml 289 ■^ Setting up faces-config.xml 290 8.2 Creating the Login page 291 Starting with HtmlGraphicImage and HtmlOutputText components 292 ■^ Adding a form 295 8.3 Sprucing things up 300 Using an image for the button 301 ■^ Integrating with JavaScript 301 ■^ Adding Cascading Style Sheets 303 8.4 Adding validators 304 Customizing validation messages 307 8.5 Improving layout with HtmlPanelGrid 308 8.6 Summary 314

9

Developing a user interface without Java code: the other pages 316 9.1 Building the header with a custom component 317 Using a custom toolbar component 321 ■^ Configuring the navigation rule 323 9.2 Prototyping data tables with panels 324 The Inbox page 325 ■^ Configuring the navigation rule 329 The Show All page 330 ■^ Configuring the navigation rule 330

xiv CONTENTS

The Approve a Project page 331 ■^ Configuring the navigation

xvi CONTENTS

13

Building an application: backing beans, security, and internationalization 499 13.1 Writing backing beans 500 Thread safety 501 ■^ Handling errors 501 ■^ Performing authentication 505 ■^ Listing projects with UIData and parameterizing listeners 511 ■^ Updating projects 522 Creating new projects 528 ■^ Paging through the project history with UIData 534 ■^ Working with JDBC ResultSets and UIData 540 13.2 Adding security 545 Container-based vs. custom security 546 Using custom security 547 13.3 Supporting internationalization in code 551 Internationalizing text with resource bundles 552 Internationalizing messages 557 13.4 Design consequences and alternatives 562 Accessing the business layer 562 ■^ Organizing beans by function 563 ■^ Action methods implemented by backing beans 564 ■^ Initializing backing bean properties with the Managed Bean Creation facility 565 13.5 Summary 566

14

Integrating JSF with Struts and existing applications 568 14.1 What integration means 569 14.2 When to use JSF with other frameworks 569 14.3 The many faces of requests and responses 571 14.4 Integrating JSF with Struts applications 572 First steps 575 ■^ Migrating Struts JSP tags 577 Using JSF action methods and managed beans 597 Who’s controlling whom? 599 14.5 Integrating JSF with non-Struts applications 600 14.6 Summary 601

CONTENTS xvii

15

ONLINE EXTENSION xix

17

RolloverButton renderer: a renderer with JavaScript support 727 17.1 Writing the RolloverButtonRenderer class 729 Encoding 731 ■^ Decoding 735 Registering the renderer 736 17.2 JSP Integration 737 Writing the HtmlBaseTag class 738 ■^ Writing the JSP custom tag 741 ■^ Validating the tag 744 ■^ Adding the tag to the tag library 745 17.3 Using the renderer 748 17.4 Wrapping an existing renderer 750 Developing the RolloverButtonDecoratorRenderer class 750 17.5 Summary 754

18

UIHeadlineViewer: a composite, data-aware component 756 18.1 RSS and the Informa API 758 18.2 Using UIData with Informa 763 18.3 Subclassing DataModel 765 18.4 Writing the UIHeadlineViewer class 768 18.5 Registering the component 780 18.6 JSP integration 781 Writing the JSP custom tag 781 Adding the tag to the tag library 787 18.7 Using the component 789 18.8 Summary 793

19

UINavigator: a model-driven toolbar component 794 19.1 Writing the model classes 796 19.2 Writing the UINavigator class 801 Implementing ActionSource methods 803 ■^ Overriding UIComponentBase methods 806 ■^ Implementing StateHolder methods 807 ■^ Developing NavigatorActionListener: a custom ActionListener 809

xx ONLINE EXTENSION

 - for checkboxes 5.5 Using HtmlSelectBooleanCheckbox 
  • 5.6 Defining item lists - for multiple items Using UISelectItem for single items 200 ■ Using UISelectItems
  • 5.7 Handling multiple-item selections - Using HtmlSelectManyCheckbox for checkbox groups - Displaying listboxes with HtmlSelectManyListbox - Using HtmlSelectManyMenu for single-item listboxes
  • 5.8 Handling single-item selections - Using HtmlSelectOneRadio for radio button groups - Using single-select listboxes with HtmlSelectOneListbox - Declaring combo boxes with HtmlSelectOneMenu
  • 5.9 Executing application commands - Declaring buttons with HtmlCommandButton - Creating an action link with HtmlCommandLink
  • 5.10 Displaying data sets with HtmlDataTable
  • 5.11 Summary - Internationalization, validators, and converters
    • 6.1 Internationalization and localization - Looking into locales 236 ■ Creating resource bundles - Using resource bundles with components - Internationalizing text from back-end code
    • 6.2 Input validation - Using validator methods 245 ■ Using validators - validators Using the standard validators 247 ■ Combining different
    • 6.3 Type conversion and formatting - standard converters Using converters 254 ■ Working with the
    • 6.4 Customizing application messages
    • 6.5 Summary
      • 9.3 Creating input forms - navigation rule 341 ■ The Create a Project page rule 337 ■ The Reject a Project page 338 ■ Configuring the - Configuring the navigation rule
      • 9.4 The Project Details page - Configuring the navigation rule
      • 9.5 Summary
        • Integrating application functionality
    • 10.1 Understanding JSF development approaches
    • 10.2 Exploring the application environment
    • 10.3 Reorganizing pages for security
    • 10.4 The Login page - Updating the navigation rule
    • 10.5 The header - Updating the navigation rule
    • 10.6 Integrating data grids - The Inbox page 370 ■ The Show All page
    • 10.7 Integrating input forms - Updating the includes 379 ■ The Approve a Project page - page The Reject a Project page 385 ■ The Create a Project
    • 10.8 The Project Details page - Updating the navigation rule
    • 10.9 Adding an error page - Updating web.xml 397 ■ Updating the navigation rule
  • 10.10 Internationalizing and localizing the UI - Externalizing text into the resource bundle - Internationalizing the header - Localizing for Russian
  • 10.11 Summary
  • P ART 3 DEVELOPING APPLICATION LOGIC CONTENTS xv - The JSF environment
    • 11.1 From servlets to JSF
    • 11.2 The application foundation - Application 413 ■ Evaluation expressions
    • 11.3 It’s all in the context - FacesContext 420 ■ FacesMessage - ExternalContext
    • 11.4 Event handling - value-change events 434 ■ Handling phase events FacesEvent 430 ■ Handling action events 432 ■ Handling
    • 11.5 Components revisited - UIComponent 442 ■ UIViewRoot - ValueHolder 449 ■ EditableValueHolder - SelectItem and SelectItemGroup model beans
    • 11.6 Summary - and foundation classes Building an application: design issues
    • 12.1 Layers of the pie
    • 12.2 Roasting the beans - breakfast 462 ■ It’s all in the properties The importance of toString 461 ■ Serialization for - Exposing beans
    • 12.3 Exploring the business layer and data layers
    • 12.4 Developing the application layer - Handling constants 478 ■ Organizing utility methods - Initializing singletons 482 ■ Adapting business objects
    • 12.5 Writing a visit object for session state
    • 12.6 Developing a base backing bean class
    • 12.7 Summary
    • VALIDATORS , AND CONVERTERS P ART 4 WRITING CUSTOM COMPONENTS , RENDERERS , - developer’s perspective The JSF environment: a component
      • 15.1 Three steps to UI extension nirvana
      • 15.2 Developing UI components - interfaces 610 ■ Event handling with method bindings Deciding when to write a UI component 608 ■ Classes and - Registration 624 ■ JSP integration
      • 15.3 Developing renderers - Deciding when to write a renderer 640 ■ Renderer - RenderKit 643 ■ Registration 644 ■ JSP integration
      • 15.4 Developing validators - Validator 649 ■ Registration 650 ■ JSP integration
      • 15.5 Developing converters - Converter 654 ■ Registration 657 ■ JSP integration
      • 15.6 Handling internationalization
      • 15.7 Packaging UI extensions
      • 15.8 Summary
  • appendix A: Using JSF without JSP - references - index - 19.3 Registering the component - 19.4 Writing the ToolbarRenderer class - Encoding 811 ■ Decoding - 19.5 Registering the renderer - 19.6 JSP integration - Writing the Navigator_ToolbarTag component tag - Writing the NavigatorItemTag tag handler - Adding the tags to the tag library - 19.7 Using the component - 19.8 Summary - Validator and converter examples - 20.1 Validator methods vs. validator classes - 20.2 Developing a validator - Writing the RegularExpressionValidator class - Registering the validator 847 ■ Integrating with JSP - Using the validator - 20.3 When custom converters are necessary - 20.4 Developing a converter - converter 865 ■ JSP integration Writing the UserConverter class 856 ■ Registering the - Using the converter - 20.5 Summary
    • appendix B: A survey of JSF IDEs and implementations
    • appendix C: Extending the core JSF classes
  • appendix D: JSF configuration
    • appendix E: Time zone, country, language, and currency codes