The Jakarta Struts Cookbook Is an Amazing Collection of Code
Total Page:16
File Type:pdf, Size:1020Kb
Jakarta Struts Cookbook By Bill Siggelkow Publisher: O'Reilly Pub Date: February 2005 ISBN: 0-596-00771-X Pages: 526 Table of • Contents • Index • Reviews • Examples The Jakarta Struts Cookbook is an amazing collection of code solutions to Reader common--and uncommon--problems encountered when building web • Reviews applications with the Struts Framework. With solutions to real-world • Errata problems just a few page flips away, this quick, look-up reference is • Academic perfect for independent developers, large development teams, and everyone in between who wishes to use the Struts Framework to its fullest potential. Jakarta Struts Cookbook By Bill Siggelkow Publisher: O'Reilly Pub Date: February 2005 ISBN: 0-596-00771-X Pages: 526 Table of • Contents • Index • Reviews • Examples Reader • Reviews • Errata • Academic Copyright Preface Audience Scope and Organization Assumptions This Book Makes Conventions Used in This Book Using Code Examples Comments and Questions Safari Enabled Acknowledgments Chapter 1. Getting Started: Enabling Struts Development Introduction Recipe 1.1. Downloading Struts Recipe 1.2. Deploying the Struts Example Application Recipe 1.3. Migrating from Struts 1.0 to Struts 1.1 Recipe 1.4. Upgrading from Struts 1.1 to Struts 1.2 Recipe 1.5. Converting JSP Applications to Struts Recipe 1.6. Managing Struts Configuration Files Recipe 1.7. Using Ant to Build and Deploy Recipe 1.8. Generating Struts Configuration Files Using XDoclet Chapter 2. Configuring Struts Applications Introduction Recipe 2.1. Using Plug-ins for Application Initialization Recipe 2.2. Eliminating Tag Library Declarations Recipe 2.3. Using Constants on JSPs Recipe 2.4. Using Multiple Struts Configuration Files Recipe 2.5. Factoring Your Application into Modules Recipe 2.6. Using Multiple Resource Bundles Recipe 2.7. Accessing Message Resources from a Database Recipe 2.8. Selectively Disabling Actions Chapter 3. User Interface Introduction Recipe 3.1. Using JSTL Recipe 3.2. Using the Struts-EL Tags Recipe 3.3. Displaying Indexed Properties Recipe 3.4. Using Indexed Properties on Forms Recipe 3.5. Using Indexed Properties in a JSTL Loop Recipe 3.6. Submitting a Form from an Image Recipe 3.7. Generating JavaScript on the Fly Recipe 3.8. Dynamically Changing Select Options Using JavaScript Recipe 3.9. Generating Dynamic Select List Options Recipe 3.10. Filtering Text Input Recipe 3.11. Generating a Set of Related Radio Buttons Recipe 3.12. Handling Unchecked Checkboxes Recipe 3.13. Handling Date Input Fields Recipe 3.14. Setting Tab Order Recipe 3.15. Generating URLs Recipe 3.16. Adding Request Parameters to a Link Recipe 3.17. Using Frames Recipe 3.18. Defeating Browser Caching Chapter 4. Tables, Sorting, and Grouping Introduction Recipe 4.1. Creating a Horizontal Bar Chart Recipe 4.2. Creating a Vertical Bar Chart Recipe 4.3. Alternating Table Row Colors Recipe 4.4. Sorting HTML Tables Recipe 4.5. Paging Tables Recipe 4.6. Using the Display Tag Library Chapter 5. Processing Forms Introduction Recipe 5.1. Creating Dynamic Action Forms Recipe 5.2. Setting DynaActionForm Initial Values Recipe 5.3. Using a List-Backed Form Property Recipe 5.4. Using a Map-Backed Form Property Recipe 5.5. Lazy Dynamic Action Forms Recipe 5.6. Populating Value Objects from ActionForms Recipe 5.7. Automatically Creating ActionForms Chapter 6. Leveraging Actions Introduction Recipe 6.1. Creating a Base Action Recipe 6.2. Relaying Actions Recipe 6.3. Returning the HTTP Response Recipe 6.4. Writing Thread-Safe Actions Recipe 6.5. Forwarding Requests Recipe 6.6. Including the Response from a Servlet or JSP Recipe 6.7. Changing the Current Module Recipe 6.8. Managing Related Operations from a Central Action Recipe 6.9. Submitting a Form from Localized Form Controls Recipe 6.10. Dispatching to Related Operations with Action Mappings Chapter 7. Execution Control Introduction Recipe 7.1. Performing Tasks at Application Startup Recipe 7.2. Tracking Client Sessions Recipe 7.3. Monitoring User Logins Recipe 7.4. Forwarding Users to Alternate Destinations Recipe 7.5. Forwarding Users to a Module Recipe 7.6. Creating a Wizard-Style Page Flow Recipe 7.7. Determining the Action Based on User Input Recipe 7.8. Using Wildcards in Action Paths Recipe 7.9. Preventing Double Form Submissions Recipe 7.10. Allowing Users to Upload Files Recipe 7.11. Displaying a File from the Server Chapter 8. Input Validation Introduction Recipe 8.1. Reusing Validator Attribute Values Recipe 8.2. Validating Using Regular Expressions Recipe 8.3. Validating Dependent Fields in Struts 1.1 Recipe 8.4. Validating Dependent Fields in Struts 1.2 Recipe 8.5. Validating an Indexed Property Recipe 8.6. Validating Dates Recipe 8.7. Validating Field Equality with a Custom Validator Recipe 8.8. Validating Field Equality in Struts 1.2 Recipe 8.9. Validating Two or More Choices Recipe 8.10. Adding a Custom Validation to a Validator Form Recipe 8.11. Validating a Wizard Form Recipe 8.12. Localizing Validation Rules Chapter 9. Exception and Error Handling Introduction Recipe 9.1. Simplifying Exception Processing in an Action Recipe 9.2. Custom Processing for Declared Exceptions Recipe 9.3. Using Exception Error Codes Recipe 9.4. Using a Global Error Page Recipe 9.5. Reporting Errors and Messages from an Action Recipe 9.6. Formatting Error Messages Chapter 10. Connecting to the Data Introduction Recipe 10.1. Accessing JDBC Data Sources from an Action Recipe 10.2. Displaying Relational Data Recipe 10.3. Mapping SQL Data to Java Objects Recipe 10.4. Integrating Struts with Hibernate Recipe 10.5. Decoupling Your Application from External Services Recipe 10.6. Integrating Spring with Struts Recipe 10.7. Loading XML Data into Your Application Recipe 10.8. Refreshing Application Data Chapter 11. Security Introduction Recipe 11.1. Securing Actions Using a Base Action Recipe 11.2. Checking for User Login on Any Struts Reques t Recipe 11.3. Securing a JSP Page Recipe 11.4. Restricting Actions by Role Recipe 11.5. Implementing "Remember Me" Logins Recipe 11.6. Ensuring Security Across Your Entire Application Recipe 11.7. Allowing a User to Log in Automatically Recipe 11.8. Limiting Access for Specific URLs by Role Recipe 11.9. Letting the Container Manage Security Recipe 11.10. Mixing Application-Managed and Container-Managed Security Recipe 11.11. Configuring Actions to Require SSL Recipe 11.12. Limiting the Size of Uploaded Files Chapter 12. Internationalization Introduction Recipe 12.1. Detecting Browser Language Settings Recipe 12.2. Sharing Message Resources with JSTL Recipe 12.3. Using an Application-Wide Locale Recipe 12.4. Changing Locale on the Fly Recipe 12.5. Creating Localized Messages from an Action Recipe 12.6. Displaying Locale-Specific Text Recipe 12.7. Displaying Locale-Specific Images Recipe 12.8. Supporting Character Sets Recipe 12.9. Localizing Look and Feel Chapter 13. Testing and Debugging Introduction Recipe 13.1. Deploying an Application Automatically Recipe 13.2. Configuring Struts Logging Recipe 13.3. Adding Logging to Your Own Classes Recipe 13.4. Enabling Remote Debugging Recipe 13.5. Troubleshooting JSP Pages Recipe 13.6. Testing Your Actions with Mock Objects Recipe 13.7. Testing Your Actions in the Container Recipe 13.8. Testing Application Functionality Chapter 14. Tiles and Other Presentation Approaches Introduction Recipe 14.1. Reusing a Common Page Layout with Tiles Recipe 14.2. Extending Tile Definitions Recipe 14.3. Displaying Tiles Using a Struts Forward Recipe 14.4. Creating Tabbed Panes Recipe 14.5. Using Tiles for I18N Recipe 14.6. Using Tiles in a Modular Application Recipe 14.7. Reusing a Common Page Layout with SiteMesh Recipe 14.8. Integrating JavaServer Faces with Struts Recipe 14.9. Integrating Struts and Velocity Recipe 14.10. Integrating Struts and XSLT Colophon Index Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. The Cookbook series designations, Jakarta Struts Cookbook, the image of the tragopan, and related trade dress are trademarks of O'Reilly Media, Inc. 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 O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Preface In just a few short years, Struts (now officially called Apache Struts) has grown from a simple idea hatched in Craig McClanahan's head into the most popular framework for building Java©-based web applications. There are other excellent Java-based web application frameworks available; many are newer than Struts, and most have benefited from lessons learned by the Struts users and developers. However, none of these frameworks offer the broad-range of features and functionality for which Struts is known. Just as important, the Struts communityone of the largest in the world of open source developmentactively supports fellow Struts users and drives Struts development. Thestruts-user mailing list often receives upwards of one hundred messages a day. Struts novices, experienced Struts users, and Struts committers are all active on this list.