Jakarta Struts Live by Richard Hightower
Total Page:16
File Type:pdf, Size:1020Kb
Jakarta Struts Live by Richard Hightower Copyright © 2004 by SourceBeat, LLC. Cover Copyright © 2004 by SourceBeat, LLC. All rights reserved. Published by SourceBeat, LLC, Highlands Ranch, Colorado. Managing Editor: James Goodwill Technical Editor: Kelly Clauson Copy Editor: Brent Barkley Layout Designer: Amy Kesic Cover Designer: Max Hays ISBN: 0974884308 Many designations used by organizations to distinguish their products are claimed as trademarks. These trademarked names may appear in this book. We use the names in an editorial fashion only with no intention of infringing on the trademark; therefore you will not see the use of a trademark symbol with every occurrence of the trademarked name. As every precaution has been taken in writing this book, the author and publisher will in no way be held liable for any loss or damages resulting from the use of information contained in this book. Table of Contents Dedication . viii Acknowledgments . ix About the Author . x Preface . xi Introduction . xii Chapter 1: Struts Quick Start Tutorial . 1 Download Struts . 3 Set up a J2EE Web Application Project That Uses Struts . 4 Write Your First Action . 5 Write Your First “Forward” . 6 Configure the Action and Forward in the Struts Configuration File . 7 Run and Test Your First Struts Application . 9 Debug Struts-Config.xml with the Struts Console . 11 Add Logging Support with Log4J and Commons Logging . 13 Write Your First ActionForm . 17 Write Your First Input View (JSP Page) . 20 Update the Action to Handle the Form and Cancel Buttons . 27 Set up the Database Pooling with Struts . 28 Exception Handling with Struts . 32 Display an Object with Struts Tags . 35 Using Logic Tags to Iterate over Users . 37 Summary . 42 Chapter 2: Testing Struts . 43 Testing Model Code with JUnit . 44 Getting Started with JUnit . 45 Using JUnit Step-by-Step . 45 Applying JUnit to Our Struts Tutorial . 51 Testing Struts Actions with StrutsTestCase . 58 Using StrutsTestCase (Mock Mode) Step-by-Step . 59 Using StrutsTestCase (Cactus Mode) . 69 Testing JSP with jWebUnit . 70 Using jWebUnit Step-by-Step . 70 Summary . 73 Chapter 3: Working with ActionForms and DynaActionForms . 74 Defining an ActionForm . 75 Understanding the Life Cycle of an ActionForm . 75 The Do’s and Don’ts of Automatic Type Conversion . 78 Jakarta Struts Live Table of Contents iv What an ActionForm Is . 79 Data Supplier: Supplies Data to html:form . 79 Data Collector: Processes Data from html:form . 79 Action Firewall: Validates Data before the Action Sees It . 79 What an ActionForm is Not . 80 Not Part of the Model or Data Transfer Object . 80 Not an Action, Nor Should It Interact with the Model . 80 Reducing the Number of ActionForms . 81 Super ActionForms . 81 Mapped Back ActionForms . 82 DynaActionForms . 83 Session vs. Request Scope ActionForms . 84 Continue the Tutorial . 85 Make User Registration a Multi-Step Process: Part 1 . 85 Create the Address JavaBean Property: Part 2 (Nested Beans) . 103 Create the Phone JavaBean Indexed Property: Part 3 (Indexed Properties) . 105 Convert the User Registration to Use DynaActionForms: Part 4 (DynaActionForms) . 107 Create the Management Feature to Edit/Delete Listings . 109 Using Dynamic Properties: Part 5 . 119 Summary . 121 Chapter 4: The Validator Framework . 122 Getting Started with the Validator Framework . 123 Common Validator Rules . 130 Mask Rule . 131 Constants . 134 Working with Dates . 135 Working with E-Mail and Credit Cards . 136 Putting It All Together . ..