Seam Framework Second Edition This page intentionally left blank Seam Framework Experience the Evolution of JavaTM EE Second Edition Michael Juntao Yuan Jacob Orshalick Thomas Heute Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States please contact: International Sales [email protected] Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data Yuan, Michael Juntao. Seam framework : experience the evolution of Java EE / Michael Juntao Yuan, Jacob Orshalick, Thomas Heute.—2nd ed. p. cm. Includes index. ISBN 978-0-13-712939-3 (pbk. : alk. paper) 1. JBoss. 2. Web servers—Management. 3. Java (Computer program language) I. Orshalick, Jacob. II. Heute, Thomas. III. Title. TK5105.8885.J42Y832 2009 005.2'762—dc22 2008047478 Copyright © 2009 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-13-712939-3 ISBN-10: 0-13-712939-4 Text printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville, Indiana. First printing, February 2009 Michael dedicates the book to Ju. Jacob dedicates the book to Jennifer and Talia. Thomas dedicates the book to Isabelle. This page intentionally left blank Contents About This Book ......................................................................................... xvii About the Authors ....................................................................................... xix Acknowledgments ....................................................................................... xxi PART I Getting Started ............................................................................................ 1 Chapter 1 What Is Seam? ............................................................................................ 3 1.1 Integrating and Enhancing Java EE Frameworks 4 1.2 A Web Framework That Understands ORM 5 1.3 Supporting Stateful Web Applications 6 1.4 Web 2.0 Ready 7 1.5 POJO Services via Dependency Bijection 7 1.6 Convention over Configuration 8 1.7 Avoiding XML Abuse 8 1.8 Designed for Testing 9 1.9 Great Tools Support 10 1.10 Let’s Start Coding! 10 Chapter 2 Seam Hello World ....................................................................................... 11 2.1 Create a Data Model 13 2.2 Map the Data Model to a Web Form 13 2.3 Handle Web Events 14 2.4 Navigate to the Next Page 15 2.5 EJB3 Bean Interface and Mandatory Method 16 2.6 More on the Seam Programming Model 17 2.6.1 Seam Built-in Components 17 2.6.2 Ease of Testing 18 2.6.3 Getter/Setter-Based Bijection 18 2.6.4 Avoid Excessive Bijection 19 2.6.5 Accessing Database via the EntityManager 20 vii viii CONTENTS 2.7 Configuration and Packaging 20 2.7.1 The WAR File 22 2.7.2 The Seam Components JAR 24 2.8 How Is This Simple? 25 Chapter 3 Recommended JSF Enhancements ........................................................... 27 3.1 An Introduction to Facelets 28 3.1.1 Why Facelets? 29 3.1.2 A Facelets Hello World 30 3.1.3 Use Facelets as a Template Engine 31 3.1.4 Data List Component 34 3.2 Seam JSF Enhancements 34 3.2.1 Seam UI Tags 34 3.2.2 Seam JSF EL Enhancement 36 3.2.3 Use EL Everywhere 37 3.2.4 Seam Filter 37 3.2.5 Stateful JSF 38 3.3 Add Facelets and Seam UI Support 38 3.4 PDF, Email, and Rich Text 40 3.4.1 Generate PDF Reports 40 3.4.2 Template-Based Email 42 3.4.3 Display Rich Text 44 3.5 Internationalization 46 Chapter 4 Seam without EJB3 ..................................................................................... 47 4.1 A Seam POJO Example 47 4.2 Configuration 48 4.3 Packaging 50 4.4 POJO Trade-Offs 52 Chapter 5 Rapid Application Development Tools ...................................................... 53 5.1 Prerequisites 54 5.2 A Quick Tutorial 54 5.2.1 Setting Up Seam-gen 54 5.2.2 Generating a Skeleton Application 57 5.2.3 Understand the Profiles 59 5.2.4 Developing the Application 61 5.2.5 Building and Deploying 61 5.2.6 Running Test Cases 63 5.3 Working with IDEs 63 5.3.1 NetBeans 63 CONTENTS ix 5.3.2 Eclipse 67 5.3.3 JBoss Tools and JBoss Developer Studio 68 5.4 Generating a CRUD Application from a Database 72 5.5 Seam-gen Command Reference 73 PART II Stateful Applications Made Easy ............................................................... 75 Chapter 6 An Introduction to Stateful Framework ................................................... 77 6.1 Correct Usage of ORM 77 6.2 Better Performance 79 6.3 Better Browser Navigation Support 81 6.4 Fewer Memory Leaks 82 6.5 High Granularity Component Lifecycle 83 6.6 Reducing Boilerplate Code 84 Chapter 7 Thinking in Components ............................................................................ 87 7.1 Stateful Components 87 7.1.1 Stateful Entity Bean 90 7.1.2 Stateful Session Bean 90 7.2 Managing Stateful Components 92 7.2.1 Stateful Component Lifecycle 92 7.2.2 Factory Methods 94 7.2.3 Manager Components 96 7.3 Configuring Components through XML 97 7.4 Page Navigation Flow 99 Chapter 8 Conversations .............................................................................................. 101 8.1 What Is a Conversation? 102 8.1.1 The Default Conversation Scope 102 8.1.2 Displaying JSF Messages 104 8.2 Long-Running Conversations 106 8.2.1 Introducing the Hotel Booking Example 106 8.2.2 The Lifecycle of a Long-Running Conversation 110 8.2.3 Conversation Timeout 111 8.3 Managing Long-Running Conversations 112 8.3.1 The Annotation Approach 112 8.3.2 The Navigation Approach 113 8.3.3 Beginning a Long-Running Conversation 115 8.3.4 Inside the Conversation 117 8.3.5 Ending a Long-Running Conversation 119 8.3.6 Links and Buttons 123 8.4 New Frontiers 124