this print for content only—size & color not accurate spine = 0.875" 376 page count EMPOWERING PRODUCTIVITY FOR THE JAVA ™ DEVELOPER US $39.99 Shelve in Java Programming User level: Beginner–Intermediate Nusairat JBoss ® Seam THE EXPERT’S VOICE ® IN JAVA ™ TECHNOLOGY Joseph Faisal Nusairat Beginning JBoss Seam From Novice to Professional CYAN MAGENTA YELLOW BLACK PANTONE 123 CV ISBN-13: 978-1-59059-792-7 ISBN-10: 1-59059-792-3 9 781590 597927 53999 Companion eBook Available Learn to build enterprise and next generation Web 2.0 applications using this powerful, open source lightweight Java ™ EE 5 application framework. www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version THE APRESS JAVA ™ ROADMAP Beginning JBoss ® Seam Practical JBoss ® Seam Web 2.0 Projects Pro Ajax and Java ™ Frameworks Beginning POJOs Beginning Hibernate Beginning JBoss ® Seam: From Novice to Professional Dear Reader, Since the late ’90s when Java ™ enterprise development started to move into full swing, there has been a steady upsurge of Java specifications (for example, JSF ™ and EJB ™ 3) and Java-based frameworks (for example, Apache Struts and Interface21 Spring). All of these were created with the idea of making life easier for the developer. However, they were always missing the glue to hold them together seamlessly. This is where JBoss ® Seam comes into the picture. Seam is a framework designed to eliminate the headache of creating middle- man objects that enable your JSF pages to talk directly to your EJB3 beans. With Seam, your JSF pages are able to call EJB3 beans without you having to create middleman beans. Seam accomplishes this in a nonintrusive manner, which still allows the running of your JSF and EJB3 components’ full life cycle. I found all this abstraction very exciting when I first read about Seam because I seemed to be spending far too much time writing repetitive code for many projects. Frameworks such as Struts were awesome at first, removing the need to write and code servlets. However, at times even these seemed to require too much repetitive behavior with the Action classes. Seam offers the abstrac- tion that frameworks such as Struts provided and brings it to the next level. I have written this book for anyone from a beginner to an expert in Java development to be able to take away a solid understanding of Seam. The first chapters are devoted to understanding the fundamental concepts of Java 5 and basic web design, as well as JSF and EJB3. From there, we delve into exploring Seam fully, including a chapter on using it with jBPM. By the end of this book, you will be able to develop and run full-fledged Java EE applications by using Seam as the connector, which will hopefully save you time. And in today’s world, time is money. Joseph Faisal Nusairat Beginning ® Join online discussions: java.apress.com Joseph Faisal Nusairat Beginning JBoss Seam From Novice to Professional ® 7923FMCMP2 2/2/07 10:47 AM Page i Beginning JBoss® Seam: From Novice to Professional Copyright © 2007 by Joseph Faisal Nusairat All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-792-7 ISBN-10 (pbk): 1-59059-792-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the U.S. and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. JBoss® is a registered trademark of Red Hat, Inc., in the U.S. and other countries. Apress, Inc., is not affili- ated with Red Hat, Inc., and this book was written without endorsement from Red Hat, Inc. Lead Editor: Steve Anglin Technical Reviewer: Floyd Carver Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Denise Santoro Lincoln Copy Edit Manager: Nicole Flores Copy Editor: Sharon Wilkey Assistant Production Director: Kari Brooks-Copony Production Editor: Lori Bring Compositor: Patrick Cunningham Proofreader: Dan Shaw Indexer: John Collin Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, email orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email info@apress.com, or visit http://www.apress.com. The infor mation in this book is distr ibuted on an “ as is ” basis , without warr anty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Do wnload section as w ell as at http://www.integrallis.com. 7923FMCMP2 2/2/07 10:47 AM Page ii To the memory of my grandparents, Kasim Nusair and Kurdeih Rashdan; To my grandparents, Henry Albert Baker and Mary Baker; To my parents, Janette Darr and AJ Nusairat; And to all my friends and family who supported me throughout the years. 7923FMCMP2 2/2/07 10:47 AM Page iii 7923FMCMP2 2/2/07 10:47 AM Page iv Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■CHAPTER 1 What Is JBoss Seam?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ■CHAPTER 3 JSF Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 ■CHAPTER 4 EJB3 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 ■CHAPTER 5 Introduction to Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 ■CHAPTER 6 Seam Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 ■CHAPTER 7 Business Process in Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 ■CHAPTER 8 Advanced Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 ■CHAPTER 9 Advanced Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 ■CHAPTER 10 Seam Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 ■APPENDIX A JBoss AS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 ■APPENDIX B JBoss IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 ■FINAL THOUGHTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 v 7923FMCMP2 2/2/07 10:47 AM Page v 7923FMCMP2 2/2/07 10:47 AM Page vi Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■CHAPTER 1 What Is JBoss Seam?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Does Seam Buy You? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Three-Tier Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Three-Tier Architecture with Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Component Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Seam Environment Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Hello World Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Introduction to MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Basics of MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Ja va 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Downloading Java 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Langua ge F eatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 POJOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Annotations on POJOs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Configuring Y our Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■CHAPTER 2 Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Contexts in Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Ser vlets and F rameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 vii 7923FMCMP2 2/2/07 10:47 AM Page vii Implementation Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Understanding the Parts of Our Examples. . . . . . . . . . . . . . . . . . . . . . 26 Displaying Dynamic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Requesting and Saving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Logging In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Listing and Viewing a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Sample Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Garage Sale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Travel Reservations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Ticketing System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 ■CHAPTER 3 JSF Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Hello World Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Using Tomahawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Configuring XML Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Creating the WAR File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Ra pid Applica tion Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 JSF Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Managed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Component Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Standard Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 JSF Expression Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Page Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Put It All T ogether . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Add Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 List Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 ■CONTENTSviii 7923FMCMP2 2/2/07 10:47 AM Page viii ■CHAPTER 4 EJB3 Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 History of EJB3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 EJB 2.x. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 EJB3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Configuring EJB3s for Deployment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Creating XML Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Stateless Session Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Sta teful Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Basics of an Entity Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Entity Bean Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Collections Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Entity Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Persistence Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Operations on the Entity Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 JPQL—EJB3 Query Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Transaction Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Calling EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 T esting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 ■CHAPTER 5 Introduction to Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 What Is Seam?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Basic Seam Configura tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Downloading Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Configuring Seam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 F irst Example: Sta teless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 ■CONTENTS ix 7923FMCMP2 2/2/07 10:47 AM Page ix [...]... 306 s APPENDIX A JBoss AS 307 What Is JBoss? 307 Downloading JBoss 307 Installing JBoss 308 Using JBoss 311 Running JBoss ... 1 1 What Is JBoss Seam? seam (sem) n A line of junction formed by sewing together two pieces of material along their margins A similar line, ridge, or groove made by fitting, joining, or lapping together two sections along their edges T he preceding definition1 of seam is usually used when discussing sewing However, this definition also fits the latest in frameworks from JBoss JBoss Seam JBoss Seam... and interesting topics related to Seam The following list outlines the contents of each chapter: Chapter 1: What Is JBoss Seam? This introductory chapter briefly explains Seam and provides an introduction to the Model View Controller (MVC) framework, Java 5, and JBoss 4 Both Java 5 and JBoss 4 are needed to run most of the applications in the book, and Java 5 is a must for Seam If you know both of them,... during this process, but also helped convert my drawings to meaningful diagrams Because this is a book on JBoss, I would be remiss not to thank Gavin King, JBoss, and the contributors to Seam for creating the Seam framework Also I would like to thank all those who contributed to the Seam Forum on the JBoss site I was a regular viewer, and I even tried to take note of items that people seemed to have trouble... functionality This chapter discusses the Stateless, Event, Page, Conversation, Session, and Application contexts Chapter 7: Business Process in Seam This chapter focuses on using JBoss Business Process Management (jBPM) with Seam jBPM is JBoss s business process management system, which usually requires custom code to interact with However, there is a Seam context specifically for Business Process components... Source Code/Download area of the Apress website (http://www.apress.com) and from my Integrallis website (http://www.integrallis.com) From the Integrallis site, click Publications and then select Beginning JBoss Seam From either site, you can download a zip file that includes the following: • Source code • Dependent library JAR files • Apache Ant build scripts • Database build scripts (when applicable)... 311 Running JBoss 311 Deploying JBoss 312 Adding a Data Source 312 Locating and Configuring Log Files 314 s APPENDIX B JBoss IDE 315 s FINAL THOUGHTS ... you have to write extensive XML files telling the presentation tier what kind of domain objects to expect By using Seam, you can remove extra coding and focus more on the parts that matter—the presentation and business logic 5 7923Ch01CMP1 6 1/19/07 1:35 PM Page 6 CHAPTER 1 s WHAT IS JBOSS SEAM? Seam does this not by creating an extra class such as a JSF action listener, but by using Java annotations... to add a bunch of extra plumbing code We will get into the details of this in later chapters (Chapters 5, 6, and 7) Hopefully, however, you can already start to see the value of not only Seam, but the combination of Seam, EJB3, and JSF Seam Environment Requirements Because Seam is dependent on JSF and EJB3, it has a few more needs than frameworks such as Spring or Struts Specifically, Seam requires... We will start with the Seam-annotated JavaBean in Listing 1-1 Listing 1-1 Our JavaBean, HelloWorldAction.java, with Seam Annotations package com.petradesigns.helloworld; import org .jboss. seam.annotations.Create; import org .jboss. seam.annotations.Name; @Name("helloWorld") public class HelloWorldAction implements IHelloWorld { private String outputText; @Create public void init() { outputText = "Hello . ROADMAP Beginning JBoss ® Seam Practical JBoss ® Seam Web 2.0 Projects Pro Ajax and Java ™ Frameworks Beginning POJOs Beginning Hibernate Beginning JBoss ® Seam:. level: Beginner–Intermediate Nusairat JBoss ® Seam THE EXPERT’S VOICE ® IN JAVA ™ TECHNOLOGY Joseph Faisal Nusairat Beginning JBoss Seam From Novice to Professional CYAN MAGENTA YELLOW BLACK PANTONE