Manning JavaServer Faces In Action Jan 2005 ISBN 1932394125 pdf

  AVA ERVER J S ACES F

IN ACTION

  Kito D. Mann Foreword by Ed Burns

  M A N N I N G

  JavaServer Faces in Action KITO D. MANN M A N N I N G

  Greenwich (74° w. long.) 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-9018 Greenwich, CT 06830 email: orders@manning.com

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

1 E

  P ART

  brief contents

  

  10

  

  9

  

  8

  

  7

  275

  

  2

  

vii

P ART

  

  6

  

  5

  

  4

  

  3

  

  2

  

  1

  1

  

  BRIEF CONTENTS

  viii ART P

  3 407

  11

  12

  13

  14

  

  4

  ART P

  

  603

  15

  

  5

  ART P

  

  16

  17

  18

  19

  20 NLINE

  XTENSION O E

  

  ix

  P ART

  

  1

  

  

  1.2 10

  

  1.3 16

  

  1.4 19

  contents

1 E

  CONTENTS

  x

  1.5

  

  1.6

  

  

  

  2.2

  

  2.3

  

  2.4

  

  2.5

  

  

  

  3.2

  

  3.3

  

  3.4

  3.5

  CONTENTS

  4.8 172

  5.4 192

  5.3

  5.2 189

  

  

  

  4.10

  

  4.9 176

  4.7 169

  xi

  4.6 167

  

  4.5

  4.4

  4.3

  4.2 148

  

  

  

  

CONTENTS

  xii

  5.5

  5.6

  

  5.7

  

  5.8 212

  

  5.9 219

  

  5.10

  5.11

  

  

  

  6.2

  

  6.3

  

  6.4 269

  6.5

  CONTENTS

  xiii P ART

  

275

2 B

  

  8.4 304

  

  9.2

  

  9.1

  

  8.6

  8.5

  

  

  7.2 281

  8.3

  

  8.2

  

  8.1 289

  

  7.5

  7.4

  7.3

  

  xiv

  CONTENTS

  9.3

  

  9.4

  

  9.5

  10.1

10 Integrating application functionality 354

  10.3

  10.8

  10.11

  

  10.10

  

  10.9

  

  

  10.4 360

  10.7 379

  

  10.6

  10.2 357

  10.5

  

  

  CONTENTS

  xv P ART

3 D

  

  12.1 457

  12.7

  12.6

  12.5

  

  12.4

  12.3 473

  

  12.2 460

  

  

  11.6 CONTENTS

   407

  11.5

  

  11.4

  

  11.3 419

  

  11.2 413

  

  xvi

  

  13.1

  

  13.2 545

  

  13.3

  

  13.4

  

  13.5

  

  

  14.1

  14.2 569

  14.3 571

  14.4

  

  14.5

  14.6

  CONTENTS

  xvii ART P

4 RITING CUSTOM COMPONENTS RENDERERS

  

   ................................ 603

  

  15.1

  15.2

  

  15.3

  

  15.4

  

  15.5

  

  15.6

  15.7 660

  15.8

   index 679

  See page xviii for contents

  online extension:

  

xviii

  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 .manning.com/mann. This is the table of contents for the online extension.

  P ART

   5

  

  

  

   ............ 703

  

  

  16.2

  16.3

  

  16.4

  16.5

  online extension

  ONLINE EXTENSION

  18.2 763

  19.2

  

  

  18.8

  18.7

  

  18.6

  18.5

  18.4

  18.3

  18.1

  xix

  

  17.5

  

  17.4 750

  17.3

  

  17.2

  

  17.1 729

  

  

  xx

  

  20.5

  

  20.4

  20.3

  

  20.2

  19.8

  ONLINE EXTENSION

  19.7

  

  19.6 822

  19.5

  

  19.4

  19.3

  

  foreword

  As I write this foreword, I am collaborating with four leading user interface SM ( UI ) component vendors on a presentation for the 2004 JavaOne conference.

  TM In our presentation, the vendors will show how they leverage JavaServer 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 UI s forced these component ven- dors 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 lead- ing 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

  xxi FOREWORD

  xxii

  problem is providing an easy-to-use UI framework built on top of a collection of technologies not well suited to UI s at all. This has led to a particularly complex specification to implement. Thankfully, the number of people actually implement- ing 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 in- depth. You will see what sets JavaServer Faces apart from other web UI frame- works, including its first-class component model, its well-defined state manage- ment 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 “relation- ship of concepts” diagram in chapter 2. This is a great way to understand the ratio- nale 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 frame- work 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

  IDE s, Kito brings to bear his experience as the principal of JSFCentral.com 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’ll find this site—and this book—extremely valuable as you explore JavaServer Faces programming.

  E D B URNS

  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.

  After two years of burning myself out and neglecting my girlfriend Tracey, I resigned and spent some time getting to know the stranger called Sleep. After we had been sufficiently reacquainted, I inhaled as much information as possible

  xxiii PREFACE

  xxiv

  about new technologies, and stumbled across the Java Specification Request ( JSR ) 127—JavaServer Faces ( JSF ). JSF seemed like an answer to the growing problem of framework mania. Moreover, since JSF abstracts so many tedious details of web application development, it seemed superior to Struts in many ways. It was clear to me that JSF was the Next Big Thing in Java web development.

  Because I was anxious to get back into writing something other than architec- ture specifications and memos, writing a book on JSF seemed like a good idea. I had reviewed several books for Manning, so I contacted the publisher to discuss writing one about JSF . After some lobbying, Manning agreed, and the JavaServer project was born. Little did we know that the specification would

  Faces in Action

  go through radical changes before its final (late) release in March 2004. (If I had a crystal ball, I would certainly not have written so much of it in early 2003; rewriting is just not fun.)

  Throughout the last year and half, a lot has happened. In May 2003, I mar- ried Tracey (I suppose leaving the start-up was a good thing after all). Later that year, I launched JSF Central, a web site dedicated to the JSF community, chock- full of resources and a handy FAQ . And finally, on March 3, 2004, JSF 1.0 was released (with the 1.1 maintenance release appearing in May). The result is a solid technology that I believe will add fire to the world of Java web develop- ment, not only by making life easier for day-to-day development but also by igniting a third-party user interface ( UI ) component industry, à la Microsoft’s

  ASP . NET Web Forms.

  To that end, I’ve worked hard to ensure that this book will serve as a catalyst on both fronts, not only helping you understand what JSF is, how it works, and how to use it, but also teaching you how to write your own UI components. I’ve also worked with representatives from Oracle,

  IBM , and Sun to paint a picture of

  how JSF is integrated into different

  IDE s. In addition, this text was influenced by

  my role as editor-in-chief of JSF Central, where I have gained a unique vantage point of the growing JSF ecosystem.

  So, there you have it. I hope JavaServer Faces in Action will inspire in you the enthusiasm that I have for this technology and serve as a useful tool in your own projects. In the meantime, I intend to reacquaint myself with that old friend, Sleep. Feel free to send your comments about this book to kmann@virtua.com or post them in the Author Online forum .manning.com/mann; once awake, I’ll be happy to read them.

  acknowledgments Most projects are not one-person endeavors, even if they start out that way.

  Technical books require the talents of an enormous range of people, many of whom donate their free time. (I will, however, take the prize for that particular sacrifice.) Let’s start at the beginning with Clay Anders, the person at Manning who believed that this book was a good idea, well before anyone else was talking about JavaServer Faces. Then, there’s Marjan Bace, Manning’s publisher, who authorized the project, then promptly ripped the first draft of my introduction to shreds. Without Marjan, this book would have already put you to sleep. (If you’re holding open your eyelids with your fingertips right now, blame him.)

  Next, there’s Jackie Carter—I couldn’t dream of a better developmental editor. Jackie didn’t just critique and guide my work; she was a true partner throughout the process. I’d also like to thank the legions of technical reviewers who let my words consume their evenings without compensation: Roland Bar- cia, Todd Cunningham, Jeff Duska, Carl Hume, Will Forster, Aleksandar Kol- undzija, Jason LeCount, Josh Oberwetter, Michael Nash, Russ Pearlman, Mark Pippins, Matthew Schmidt, Keyur Shah, Sang Shin, and Henri Yandell. Ted Kennedy (rest in peace) and Dave Roberson deserve mention for doing a com- mendable job coordinating the review process.

  Several members of the JSF Expert Group (Eric Lazarus, Brian Murray, Adam Winer, and especially Michael Nash) helped ensure the technical accu- racy of this book, and Ed Burns (the co-spec lead) wrote an excellent foreword.

  xxv ACKNOWLEDGMENTS

  xxvi

  Along the same lines, several vendors gave me insight into their products well before they had public releases available. Beverly Dewitt provided general infor- mation about

  IBM WebSphere Studio, and Roland Barcia wrote the portion of

  online extension appendix B about that product (in addition to providing an extremely thorough technical review). Jim Inscore from Sun Microsystems made sure I had access to early releases of Java Studio Creator.

  Jonas Jacobi and his team at Oracle (Brian Albers, John Fowler, and Adam Winer) went above and beyond the call of duty by not only providing details about JDeveloper but also conducting informal reviews and serving as a general

  

JSF resource. Jonas also wrote about JDeveloper’s JSF support and Oracle’s ADF

Faces Components in online extension appendix B.

  In addition, I enlisted the assistance of my good friend Ed LaCalle for Span- ish translations, as well as my world-traveling brother, John A. Mann II, for Rus- sian translations. My wife Tracey Burroughs provided general technical guidance and support. She also wrote most of appendix E—she has an uncanny knack for detail, and she’s way too brilliant for her own good.

  There’s also the production team, who worked to create the early-access ver- sion of this book (available in PDF ) as well as the print version. Henri Yandell is an extremely meticulous technical editor who made sure that everything I wrote made sense. Liz Welch, the copyeditor, corrected all of my bad grammar and made sure I followed Manning’s guidelines. Susan Forsyth proofread every word, Denis Dalinnik typeset every single page, and Susan Edwards tackled the extremely tedious job of indexing. Finally, Mary Piergies coordinated the pro- duction process.

  If you have seen this book mentioned on a web site or received a free copy or excerpt, you have Helen Trimes to thank—she’s done an outstanding marketing job. I’d also like to thank a few others at Manning: Lianna J. Wlasiuk, Susan Cap- parelle, Syd Brown, and Iain Shigeoka.

  And then there are my parents—the strongest and kindest people I know. Whenever I look at where I am in life, I can see a profound imprint of their love and encouragement.

  All of these people worked hard to make JavaServer Faces in Action a quality product. Lastly, I’d like to thank you, the reader, for buying this book. (Okay, maybe you’re just flipping through it, but that counts for something too.)

  about this book

  This book is written for people who currently develop Java web applications— architects, application developers, and front-end developers. From my per- spective, architects worry about the application’s design, which technologies are used, and how the development process will work. Application developers build model objects, application logic, and so on, while front-end developers concentrate on building the GUI , usually with a display technology like Java- Server Pages ( JSP ) or Velocity. In many shops, these roles are performed by the same people, or those in different roles have overlapping skill sets. JSF is a web application framework like Struts, WebWork, or Tapestry, but this book is accessible even if you haven’t used a web framework before.

  JavaServer Faces in Action is divided into five parts. The first part covers JSF basics. Chapter 1 explains the motivation behind JSF , examines how it fits into the current landscape, and has the requisite Hello, world! example. This chapter also provides a brief overview of the foundation technologies JSF uses:

  

HTTP , servlets, portlets, and display technologies like JSP . Chapter 2 delves

  further into JSF by examining its core concepts and explaining in detail how

  

JSF performs its magic. Chapter 3 covers everyday topics like configuration,

JSP integration, JavaBean initialization, and navigation. Chapters 4 and 5

  cover the standard UI components, and chapter 6 examines internationaliza- tion, validation, and type conversion. All of these chapters explain JSF as a technology, but also reveal how it is used within tools.

  xxvii ABOUT THIS BOOK

  xxviii Part 2 is focused on building a UI using all of the concepts outlined in part 1.

  It begins with chapter 7, which introduces the case study that is used throughout parts 2 and 3. Chapters 8, 9, and 10 build a working prototype of the case study using UI components and JSP without any Java code.

  Part 3 focuses on the application code required to turn the prototype into a real application. Chapter 11 outlines JSF ’s Java API from an application developer’s perspective, while chapters 12 and 13 walk through design and development of the application. Chapter 14 examines integration with existing frameworks like Struts.

  Part 4 looks at the other side of the coin: extending JSF with UI components, renders, validators, and converters. Chapter 15 examines the JSF API from a component developer’s perspective. The print edition ends with appendix A, which looks at using JSF without JSP .

   UI component, and chapter 17 examines render- ers. Chapters 18 and 19 show how to build more sophisticated UI components, and chapter 20 walks through developing a validator and a converter. All of these chapters use examples that are applicable to everyday web development.

  The online extension ends with four appendices that cover a range of addi- tional topics. Appendix B provides thorough coverage of JSF support in Oracle JDeveloper,

  IBM WebSphere Studio, and Sun Java Studio Creator. Appendix C

  looks more closely at JSF ’s architecture and shows how to extend it with pluggable classes. The last two appendices are references: appendix D covers every config- uration element, and appendix E lists time zone, language, and country codes.

  How to use this book

  This book is part tutorial, part case study, and part reference. It’s written so that it can be read sequentially, but I have tried to ensure that individual sections make some sense in isolation. That being said, if you have the time, just start from the beginning and skip the sections that don’t seem relevant. Be careful skipping entire chapters, because each chapter may have a nugget of informa- tion that you find useful.

  Of course, few people believe that they have any time (let alone the time), so here are a few guidelines. If you’re an architect, you should peruse most of this book, but pay close attention to chapters 1, 2, 3, 6, 8, 12, 13, and 15. You may also want to peruse appendix A, and online extension appendices B and C.

  ABOUT THIS BOOK

  xxix

  Application developers should read most of parts 1–3, but you can skim chap- ters 4 and 5. You only need to read chapter 14 if you’re working with an existing web application, or if you’re currently using Struts. Advanced developers should certainly read parts 4 and 5 (online), as well as appendix A, and online extension appendices B and C.

  Front-end developers should read all of parts 1 and 2, with the possible exception of parts of chapter 2. In general, this book becomes more complicated as you get closer to the end.

  References

  References to web sites, books, and articles are denoted in brackets ([]) and can be found in the References section at the end of this book. For example, the author’s community site, JSF Central [ JSF Central] is a great place to find out more information about JSF news, products, and resources. In the References section, the bracketed text maps to the actual URL : [ JSF Central] JSF Central community web site, http://www.jsfcentral.com.

  The bracketed name looks the same regardless of whether it’s a web site, a prod- uct, a book, or an article.

  Conventions

  Like in any good book, this text is mostly self-explanatory. I do, however, use a few conventions, which are explained in the following sections.

  Boldface type

  I use boldface type to emphasize portions of code segments (which could be in Java,

  XML , or JSP ). Usually I’m trying to point out the meat of the code or draw attention to text that was added to a previous listing.

  Italicized type

  Italics are used when I’m defining a word. I also use them to emphasize particu- lar words, in the usual way.

  Courier type like

  Courier type ( this ) is used for code (Java,

  XML , or JSP ). I use it in code list-

  ings, but also for Java class names,

  XML tags, and anything else you might type into a development tool. ABOUT THIS BOOK

  xxx Component tables

  In chapters 4 and 5, I use specialized tables to describe JSF UI components. The first is a Component Summary table; here’s an example:

  HtmlOutputText

  summary HtmlOutputText

  Component Family javax.faces.Output

  Possible IDE Output Text

  Display Names Converts the value to a string and displays it with optional support CSS styles. (If the id

  Display or style property is set, encloses the text in a <span> element.) Behavior

  Tag Tibrary HTML <h:outputText>

  JSP Tag Pass-Through style, title

  Properties Common id, value, rendered, converter, styleClass, binding (see table 4.2)

  Properties Default

  Property Type Required? Description Value escape boolean true No Controls whether or not HTML or XML characters are escaped (displayed literally in a browser).

  Don’t worry about what this means quite yet—the point is that all of the standard

  

JSF UI components are described this way. The idea is to give you all of the basic

details about the component in one single table.

  UI component examples are handled in tables as follows: HtmlOutputText example: Text is escaped by default.

  HTML What are <i>you</i> looking at? <h:outputText value="What are <i>you</i> looking at?"/>

  Component Tag Browser Display

  ABOUT THIS BOOK

  xxxi

  Here, I show the HTML output, the JSP component tag, and the browser display, all in one table. This way, you can easily see how the three different pieces are related. The HTML is displayed first for those who think in terms of HTML .

  Code annotations

  I use code annotations because they look much cooler than comments. Here’s an example: public String myAction() This is an

  b

action method

  { // Do something }

  Sometimes I’ll expand upon annotations in paragraphs after the code listing,

  b using numbered cueballs like this: .

  Callouts

  I use the typical callouts like N OTE , W ARNING , D EFINITION , and so on through- out the text to emphasize specific points that may otherwise get buried in ordi- nary text. Here’s an example:

  DEFINITION A UI component, or control, is a component that provides specific func- tionality for interacting with an end user. Classic examples include tool- bars, buttons, panels, and calendars.

  In addition, I use a couple of unique ones:

BY THE

  Makes a point that isn’t essential to the current text, but may be useful WAY anyway. Usually, I’m attempting to address related questions you may have.

  SOAPBOX My own personal opinion. Take these with a grain of salt.

  Source code and the online extension

  All of the source code for this book can be downloaded from the book’s web site: http://www.manning.com/mann. The downloadable files contain instructions for installation and compilation. From the same site, book owners can also download an additional 300 pages of this book, called the online extension, in PDF format. ABOUT THIS BOOK

  xxxii Author Online

  Manning maintains a forum for us authors to spew our biased views at our beloved readers. The one for JavaServer Faces in Action is available at http:// www.manning.com/mann. If you have any questions about JSF or comments about this book, feel free to drop by. I will personally be checking this forum from time to time, as I’m keenly interested in what you have to say.

  About the author

  Kito D. Mann is a consultant specializing in enterprise architecture, mentoring, and development. A programmer since the tender age of 12, he has written sev- eral articles on Java-related technologies, and also speaks at user groups and conferences. He has consulted with several Fortune 500 companies, and has been the chief architect of an educational application service provider. Kito is also the founder of the JSF Central community web site, and a member of JSF 1.2 and JSP 2.1 expert groups. He holds a B.A. in Computer Science from Johns Hopkins University, and lives in Stamford, Connecticut, with his wife, four cats, and two parrots. In his spare time, he enjoys making incomplete compositions with electronic music equipment.

  about the title and cover About the title

  By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover dur- ing self-motivated exploration.

  Although no-one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action book is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

  There is another, more mundane, reason for the title of this book: our read- ers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

  xxxiii ABOUT THE TITLE AND COVER

  xxxiv About the cover illustration

  The figure on the cover of JavaServer Faces in Action is a “Saka,” or a Turkish water carrier. The illustration is taken from a collection of costumes of the Ottoman Empire published on January 1, 1802, by William Miller of Old Bond Street, Lon- don. The title page is missing from the collection and we have been unable to track it down to date. The book’s table of contents identifies the figures in both English and French, and each illustration bears the names of two artists who worked on it, both of whom would no doubt be surprised to find their art gracing the front cover of a computer programming book...two hundred years later.

  The collection was purchased by a Manning editor at an antiquarian flea mar- ket in the “Garage” on West 26th Street in Manhattan. The seller was an Ameri- can based in Ankara, Turkey, and the transaction took place just as he was packing up his stand for the day. The Manning editor did not have on his person the substantial amount of cash that was required for the purchase and a credit card and check were both politely turned down.

  With the seller flying back to Ankara that evening the situation was getting hopeless. What was the solution? It turned out to be nothing more than an old- fashioned verbal agreement sealed with a handshake. The seller simply proposed that the money be transferred to him by wire and the editor walked out with the bank information on a piece of paper and the portfolio of images under his arm. Needless to say, we transferred the funds the next day, and we remain grateful and impressed by this unknown person’s trust in one of us. It recalls something that might have happened a long time ago.

  The pictures from the Ottoman collection, like the other illustrations that appear on our covers, bring to life the richness and variety of dress customs of two centuries ago. They recall the sense of isolation and distance of that period‹and of every other historic period except our own hyperkinetic present.

  Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one conti- nent from another. Perhaps, trying to view it optimistically, we have traded a cul- tural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

  We at Manning celebrate the inventiveness, the initiative, and, yes, the fun of the computer business with book covers based on the rich diversity of regional life of two centuries ago brought back to life by the pictures from this collection.

  Part 1 Exploring JavaServer Faces art 1 introduces and explores the world of JavaServer Faces programming. P We provide an overview of the technology and explain how JSF fits into the

  current web development landscape. Next, we discuss a sample application, explore how JSF works, and examine its fundamental concepts. This part of the book concludes with chapters that cover all of the standard JSF compo- nents, as well as features like internationalization and validation.

  3 Introducing JavaServer Faces

  This chapter covers What JavaServer Faces is, and what it’s not Foundation technologies (HTTP, servlets, portlets, JavaBeans, and JSP) How JavaServer Faces relates to existing web development frameworks Building a simple application

  CHAPTER 1

4 Introducing JavaServer Faces

  Welcome to JavaServer Faces in Action. JavaServer Faces ( JSF , or simply “Faces”) makes it easy to develop web applications by bringing support for rich, powerful user interface components (such as text boxes, list boxes, tabbed panes, and data

  1 grids) to the web development world. A child of the Java Community Process,

  JSF is destined to become a part of Java 2 Enterprise Edition ( J2EE ). This book

  will help you understand exactly what JSF is, how it works, and how you can use it in your projects today.

1.1 It’s a RAD-ical world

  A popular term in the pre-Web days was Rapid Application Development ( RAD ). The main goal of RAD was to enable you to build powerful applications with a set of reusable components. If you’ve ever used tools like Visual Basic, PowerBuilder, or Delphi, you know that they were a major leap forward in application develop- ment productivity. For the first time, it was easy to develop complex user inter- faces ( UI s) and integrate them with data sources.

  You could drag application widgets— UI controls and other components— from a palette and drop them into your application. Each of these components

  font

  had properties that affected their behavior. (For example, is a common

  dataSource

  property for any control that displays text; a data grid might have a property to represent a data store.) These components generated a set of events, and event handlers defined the interaction between the UI and the rest of the application. You had access to all of this good stuff directly from within the inte- grated development environment (

  IDE ), and you could easily switch between design and code-centric views of the world. RAD tools were great for developing full-fledged applications, but they were

  also quite useful for rapid prototyping because they could quickly create a UI with little or no code. In addition, the low barrier to entry allowed both experi- enced programmers and newbies to get immediate results.

  These tools typically had four layers: An underlying component architecture A set of standard widgets An application infrastructure The tool itself

1 The Java Community Process (JCP) is the public process used to extend Java with new application programming interfaces (APIs) and other platform enhancements. New proposals are called Java Specification Requests (JSRs).

  It’s a RAD-ical world

  5 The underlying component architectures were extensible enough to spawn an in- dustry of third-party component developers like Infragistics and Developer Express.

  Of course, the RAD philosophy never went away—it just got replaced by other hip buzzwords. It’s alive and well in some Java

  IDE s and other development envi-

  ronments like Borland Delphi and C++Builder. Those environments, however, stop short of using RAD concepts for web projects. The adoption of RAD in the web development world has been remarkably slow.

  This sluggishness is due in part to the complexity of creating such a simple, cohesive view of application development in a world that isn’t simple or cohe- sive. Web applications are complex if you compare them to standard desktop applications. You’ve got a ton of different resources to manage—pages, configu- ration files, graphics, and code. Your users may be using different types of brows- ers running on different operating systems. And you have to deal with HTTP , a protocol that is ill suited for building complex applications.

  The software industry has become good at masking complexity, so it’s no sur- prise that many RAD web solutions have popped up over the last few years. These solutions bring the power of visual, component-oriented development to the complex world of web development. The granddaddy is Apple’s WebOb-

  2 jects, and Microsoft has brought the concept to the mainstream with Visual Stu- dio. NET and ASP . NET Web Forms. In the Java world, many frameworks have emerged, several of them open source. Some have tool support, and some don’t.

  However, the lack of a standard Java RAD web framework is a missing piece of the Java solution puzzle—one that Microsoft’s. NET Framework has covered from day one. JavaServer Faces was developed specifically to fill in that hole.

1.1.1 So, what is JavaServer Faces?

  In terms of the four layers of a RAD tool, JavaServer Faces defines three of them: a component architecture, a standard set of UI widgets, and an application infra- structure. JSF ’s component architecture defines a common way to build UI widgets. This architecture enables standard JSF UI widgets (buttons, hyperlinks, check- boxes, text fields, and so on), but also sets the stage for third-party components. Components are event oriented, so JSF allows you to process client-generated events (for instance, changing the value of a text box or clicking on a button).

  Because web-based applications, unlike their desktop cousins, must often appease multiple clients (such as desktop browsers, cell phones, and PDA s), JSF

2 WebObjects has a full-fledged environment that includes a J2EE server, web services support, and ob- ject persistence, among other things.

  CHAPTER 1

6 Introducing JavaServer Faces

Figure 1.1 IBM’s WebSphere Application Developer (WSAD) has been expanded to support JSF applications in addition to the seemingly endless amount of other technologies it supports. You can

  visually build JSF applications, and mix-and-match other JSP tag libraries using WSAD’s familiar Eclipse-based environment. has a powerful architecture for displaying components in different ways. It also has extensible facilities for validating input (the length of a field, for example) and converting objects to and from strings for display.

  Faces can also automatically keep your UI components in synch with Java objects that collect user input values and respond to events, which are called . In addition, it has a powerful navigation system and full support

  backing beans

  for multiple languages. These features make up JSF ’s application infrastruc- ture—basic building blocks necessary for any new system.

  JavaServer Faces defines the underpinnings for tool support, but the imple- mentation of specific tools is left to vendors, as is the custom with Java. You have

  It’s a RAD-ical world

  7 Figure 1.2 Oracle’s JDeveloper [Oracle, JDeveloper] will have full-fledged support for JSF, complete

  with an extensive array of UIX components, which will integrate with standard JSF applications. It will also support using JSF components with its Application Development Framework (ADF) [Oracle, ADF]. (This screen shot was taken with UIX components available with JDeveloper 10g, which are the basis of JSF support in the next version of JDeveloper.)

  UI