JavaServer Faces JSF in Action

JavaServer Faces JSF in Action

(Parte 1 de 5)

Kito D. Mann Foreword by Ed Burns

JavaServer Faces in Action

Greenwich (74° w. long.)

For online information and ordering of this and other Manning books, please go to 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 AvenueFax: (203) 661-9018 Greenwich, CT 06830email:

©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 AvenueTypesetter:Denis Dalinnik Greenwich, CT 06830Cover designer:Leslie Haimes

ISBN 1-932394-1-7

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.




brief contents







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 w.

foreword xxi preface xi acknowledgments xxv about this book xxvii about the title and cover xi


Hypertext Transfer Protocol (HTTP) 1 Servlets 12 Portlets 13 JavaBeans 14 JSP and other display technologies 15

Why do we need frameworks? 16 She’s a Model 2 17 JSF, Struts, and other frameworks 18 contents x CONTENTS

Dissecting hello.jsp 24 Dissecting goodbye.jsp 31 Examining the HelloBean class 32 Configuration with faces-config.xml 34 Configuration with web.xml 36

User interface components 41 Renderers 43 Validators 4 Backing beans 45 Converters 48 Events and listeners 49 Messages 5 Navigation 56

Phase 1: Restore View 61 Phase 2: Apply Request Values 63 Phase 3: Process Validations 65 Phase 4: Update Model Values 6 Phase 5: Invoke Application 6 Phase 6: Render Response 68

Naming containers 72 Referencing identifiers 73 2.4Exploring the JSF expression language 76

Understanding scoped variables 80 Using implicit variables 81 Using the EL with components 83

Basic requirements 89 Choosing a JSF implementation 89 Directory structure 90 Configuration 92

Using JSP includes 103 Using JSF with JSTL and other JSP custom tags 104

Declaring managed beans 113 Declaring Lists and Maps as managed beans 123 Setting values with value-binding expressions 125


Using HTML attributes 142 Understanding facets 143 The power of tools 145 The render kit behind the scenes 148

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

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 xii CONTENTS

Using UISelectItem for single items 200 Using UISelectItems for multiple items 203

Using HtmlSelectManyCheckbox for checkbox groups 205 Displaying listboxes with HtmlSelectManyListbox 208 Using HtmlSelectManyMenu for single-item listboxes 210

Using HtmlSelectOneRadio for radio button groups 212 Using single-select listboxes with HtmlSelectOneListbox 215 Declaring combo boxes with HtmlSelectOneMenu 217

Declaring buttons with HtmlCommandButton 219 Creating an action link with HtmlCommandLink 221

Looking into locales 236 Creating resource bundles 238 Using resource bundles with components 241 Internationalizing text from back-end code 244

Using validator methods 245 Using validators 246 Using the standard validators 247 Combining different validators 251

Using converters 254 Working with the standard converters 255



8 Developing a user interface without Java code: the Login page 287

Starting with HtmlGraphicImage and HtmlOutputText components 292 Adding a form 295

Using an image for the button 301 Integrating with JavaScript 301 Adding Cascading Style Sheets 303

9 Developing a user interface without Java code: the other pages 316

Using a custom toolbar component 321 Configuring the navigation rule 323

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 rule 337 The Reject a Project page 338 Configuring the navigation rule 341 The Create a Project page 341 Configuring the navigation rule 347

The Inbox page 370 The Show All page 378 10.7Integrating input forms 379

Updating the includes 379 The Approve a Project page 382 The Reject a Project page 385 The Create a Project page 386

Updating web.xml 397 Updating the navigation rule 397 10.10Internationalizing and localizing the UI 398

Externalizing text into the resource bundle 398 Internationalizing the header 400 Localizing for Russian 402



Application 413 Evaluation expressions 417 1.3It’s all in the context 419

FacesContext 420 FacesMessage 422 ExternalContext 424

FacesEvent 430 Handling action events 432 Handling value-change events 434 Handling phase events 435

UIComponent 442 UIViewRoot 446 ValueHolder 449 EditableValueHolder 451 SelectItem and SelectItemGroup model beans 453

The importance of toString 461 Serialization for breakfast 462 It’s all in the properties 462 Exposing beans 472

Handling constants 478 Organizing utility methods 480 Initializing singletons 482 Adapting business objects 484


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

Internationalizing text with resource bundles 552 Internationalizing messages 557

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

14 Integrating JSF with Struts and existing applications 568

First steps 575 Migrating Struts JSP tags 577 Using JSF action methods and managed beans 597 Who’s controlling whom? 599




Deciding when to write a UI component 608 Classes and interfaces 610 Event handling with method bindings 623 Registration 624 JSP integration 627

Deciding when to write a renderer 640 Renderer 641 RenderKit 643 Registration 644 JSP integration 647

Validator 649 Registration 650 JSP integration 652 15.5Developing converters 654

Converter 654 Registration 657 JSP integration 658 15.6Handling internationalization 660

appendix A:Using JSF without JSP 665 references 675 index 679 online extension:See page xviii for contents


The online extension consists of five chapters in part 5 as well as four appendixes that are not included in the print edition. They are available for free download from the book’s web site at This is the table of contents for the online extension.



Encoding 709 Decoding 715 Implementing StateHolder methods 717

Writing the JSP custom tag 718 Validating the tag 721 Adding the tag to the tag library 722 online extension


Encoding 731 Decoding 735 Registering the renderer 736

Writing the HtmlBaseTag class 738 Writing the JSP custom tag 741 Validating the tag 744 Adding the tag to the tag library 745

18 UIHeadlineViewer: a composite, data-aware component 756

Writing the JSP custom tag 781 Adding the tag to the tag library 787

Implementing ActionSource methods 803 Overriding UIComponentBase methods 806 Implementing StateHolder methods 807 Developing NavigatorActionListener: a custom ActionListener 809 xxONLINE EXTENSION

Encoding 811 Decoding 820 19.5Registering the renderer 821

Writing the Navigator_ToolbarTag component tag 822 Writing the NavigatorItemTag tag handler 826 Adding the tags to the tag library 831

Writing the RegularExpressionValidator class 842 Registering the validator 847 Integrating with JSP 847 Using the validator 852

Writing the UserConverter class 856 Registering the converter 865 JSP integration 866 Using the converter 870 appendix B: A survey of JSF IDEs and implementations 873 appendix C: Extending the core JSF classes 935 appendix D: JSF configuration 958 appendix E: Time zone, country, language, and currency codes 976 xxi foreword

As I write this foreword, I am collaborating with four leading user interface (UI) component vendors on a presentation for the 2004 JavaOneSM conference. In our presentation, the vendors will show how they leverage JavaServerTM Faces technology in their products. While developing the presentation, I am learning some things about the work we’ve been doing on JavaServer Faces for the past three years. The vendors have their own set of concerns unique to adapting their product for JavaServer Faces, but they all voice one opinion loud and clear: they are very relieved to finally have a standard for web-based user interfaces.

The absence of a standard for web-based UIs forced these component vendors to write special case code for every integrated development environment (IDE) into which they wanted to plug. Now that we have the JavaServer Faces standard, any IDE can declare compliance to that standard, and any vendor that also complies with the standard can plug components into the IDE with much less work. Of course, this means that any components you develop will also be able to plug into tools without too much additional work.

The JavaServer Faces specification was developed by a community of leading minds in the field of web UI development. We took the best ideas from many different approaches to UI frameworks and assembled them into one coherent whole. The trouble with standards is that they get rather complex in order to solve the problems they are addressing. For JavaServer Faces, that xi FOREWORD problem is providing an easy-to-use UI framework built on top of a collection of technologies not well suited to UIs at all. This has led to a particularly complex specification to implement. Thankfully, the number of people actually implementing the spec is relatively small compared to those using those implementations, but as it turns out, knowing the specification in detail is still helpful in order to use it.

As a member of the expert group developing the next version of JavaServer

Pages, Kito is no stranger to technology standards. Kito grasps the key value-adds of JavaServer Faces and has explained them in a book that is accessible and indepth. You will see what sets JavaServer Faces apart from other web UI frameworks, including its first-class component model, its well-defined state management system, and its conceptual similarity to JavaBeans. Kito is familiar with the abstractions being used by the specification, and, more important, he understands why we used those abstractions. Understanding the why of the specification leads to a superior explanation for you, the reader. For example, look at the “relationship of concepts” diagram in chapter 2. This is a great way to understand the rationale for the design of JavaServer Faces.

Kito also understands the marketplace into which this technology fits. This means you get the most important information first, so you can get your job done quickly and completely. He spends just enough time building a firm foundation of the technology underlying JavaServer Faces, making the book ideal for getting started from scratch.

Finally, the book has all the things you’ve come to expect from a top-quality software technology book: a descriptive table of contents that can serve as a framework for understanding, chapter goals and summaries to save you time, and lots of working examples that you can use in your own projects. One thing I’ve seen in this book that I haven’t seen in others is an in-depth look at the currently shipping IDEs that support JavaServer Faces. This is especially valuable because such tools can save you time, once you understand the underlying technology they support.

In addition to the unique insight this book offers on shipping IDEs, Kito brings to bear his experience as the principal of to inform the entire book. This site is a high-quality aggregation of articles, interviews, and, most important, an up-to-the-minute picture of the state of industry and community offerings related to JavaServer Faces. Kito has separate sections that cover components, render kits, implementations, and more. I think you’l find this site—and this book—extremely valuable as you explore JavaServer Faces programming.

ED BURNS JavaServer Faces Specification Lead preface

I’ve always enjoyed writing. I was one of those computer geeks who found humanities classes easier, in some ways, than computer science courses—that is, if I could manage to convince my professors that I had actually read the dozens of books they gave me. In the late 1990s, I finally fused my affection for writing with my obsession for software development by writing magazine articles and performing technical book reviews. Then, in 2000, after years of financial consulting with Fortune 500 companies, I took the start-up plunge.

Okay, so it was a little late. But I was jealous of my friends who had joined start-ups and intrigued by all of those wonderful stories in magazine articles. The start-up I chose was an educational application service provider (recently acquired by McGraw-Hill) that, surprisingly, had a real business plan. A key part of our product was a web-based application, and one of my tasks, as chief architect, was to build it (with, of course, the help of other poor souls). Instead of using my own homegrown web application framework, I chose Struts, which at the time was a good decision. As I directed development through a couple of release cycles, I gained an understanding of the good and bad parts of Struts, as well as the issues involved with developing a mission-critical web application with team members who all had different backgrounds.

(Parte 1 de 5)