Frederic Daoud — «Stripes... and Java Web Development Is Fun Again
Total Page:16
File Type:pdf, Size:1020Kb
What readers are saying about Stripes. and Java Web Development Is Fun Again This book is a must for anyone using Stripes, novice or pro. The author has done a great job of explaining the basics as well as the details of Stripes’ amazing features while showing how to build a real- life application. A novice developer can get up to speed fast, keeping with Stripes’ pragmatic approach to development: “It doesn’t have to be hard.” As the chapters progress, you will gain thorough knowledge of all the Stripes features. What really impressed me was the author’s dedication to giving you full examples of all the possible variations; you’re not left thinking, “If I just knew how to use that feature.” If you want to know how to use a Stripes feature, look it up in this book—it’s definitely covered. Stripes...and Java Web Development Is Fun Again will be on my work desk from now on. Jeppe Cramon Chief Architect, TigerTeam This book is really engaging. Since I’m familiar with Stripes, I enjoyed learning about many lesser-known nuances that Stripes provides— and those tasty little nuggets kept me reading. This book delivers a comprehensive understanding of the intellectual and technical aspects of Stripes. It has served to cement my appreciation for Stripes. Brandon Goodin Coauthor, iBATIS in Action At first I thought this book would be merely a welcome dead-tree ref- erence for our team of self-proclaimed veteran Stripes developers. But somewhere along the way Frederic Daoud managed to greatly impress and humble me with his experience and in-depth knowledge of Stripes. With its clear and well-structured chapters, the content is thorough and fast-moving. The text flows naturally from topic to topic and from chapter to chapter. The code is good, clear, and readable. The tone is personal and light, like the author is casually chatting with us, the readers. This was very appealing to me, having read truckloads of dry, almost scientific comp-sci books. And even I, as a long-time Stripes user, learned some things I didn’t know. Jasper Fontaine Lead Developer, Codegap As a Stripes committer, I learned a good bit from reading this book. For example, the explanation of how checkboxes are handled is very informative. In fact, I might print it out and hang it above my desk! Even seasoned Stripes developers get confused by that sometimes. Thanks, Frederic. Ben Gunter Committer, The Stripes Framework I changed several of my practices after reading this book. The book contains some really great ideas, and I really was surprised I learned so much from it! Aaron Porter Committer, The Stripes Framework This is a valuable resource to both newcomers and experienced Stripes developers. Although Stripes is very easy to use, this is the book I wish I had read when I got started! Having used Stripes for more than a year, I still found many great tips that will help me improve and simplify my code. Chris Herron Freelance Java Developer Stripes . and Java Web Development Is Fun Again Frederic Daoud with Tim Fennell The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Frederic Daoud. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-21-2 ISBN-13: 978-1-934356-21-0 Printed on acid-free paper. P1.0 printing, October 2008 Version: 2009-4-20 To Nadia. You are the love of my life and the woman of my dreams. Contents 1 Introduction 13 1.1 What Can Stripes Do for You? ............... 15 1.2 Getting the Most Out of This Book ............ 16 1.3 Acknowledgments ...................... 19 I Learning the Controls 21 2 Stripes 101: Getting Started 22 2.1 Setting Up a Stripes Application ............. 22 2.2 Hello, Stripes! ........................ 27 2.3 Binding to Action Beans .................. 36 2.4 Wrapping Up ......................... 39 3 The Core: Action Beans and JSPs 40 3.1 Let’s CRUD .......................... 41 3.2 Writing a Base for a Stripes Application ......... 43 3.3 Displaying Data with Action Beans and JSPs . 46 3.4 Parameterized Links .................... 52 3.5 Displaying Messages to the User ............. 57 3.6 Creating Forms ....................... 60 3.7 Use a Forward or a Redirect? ............... 67 4 Validating User Input 70 4.1 Stripes Validation Concepts ................ 70 4.2 Using Built-in Validations ................. 76 4.3 When You Need More: Custom Validation Methods . 88 5 There’s More to Life Than Strings: Working with Data Types 98 5.1 Type Conversion Concepts ................. 98 5.2 Built-in Type Converters .................. 100 5.3 Formatting .......................... 107 5.4 Working with Custom Data Types ............. 110 CONTENTS 10 6 Customizing Stripes Messages 121 6.1 Customizing Information Messages ............ 121 6.2 Customizing Error Messages ............... 124 6.3 Changing the Text of Error Messages .......... 131 6.4 Creating Messages for Custom Errors .......... 138 7 Reusable Layouts 141 7.1 Basic Stripes Layout Concepts .............. 142 7.2 Putting Layouts to Work: Decorators ........... 146 7.3 Using View Helpers ..................... 156 7.4 If You’re Used to Tiles or SiteMesh ............ 161 II Revving Up 165 8 Adding Form Input Controls 166 8.1 Checkboxes ......................... 167 8.2 Select Boxes ......................... 170 8.3 Image Buttons and Text Areas .............. 174 8.4 Using Cross-page Controls ................. 178 8.5 Radio Buttons ........................ 180 9 Advanced Features Made Easy 183 9.1 Managing Session Data ................... 183 9.2 Altering Form Values in the Action Bean ........ 188 9.3 Using Indexed Properties .................. 191 9.4 Working with Files ..................... 195 10 Registering and Logging In 203 10.1 The Registration Page .................... 203 10.2 Adding Password and Confirm Password Boxes . 205 10.3 Dealing with a Bunch of Required Fields ........ 207 10.4 Using Validation Metadata ................. 208 10.5 Creating a Wizard ...................... 210 10.6 The Login Page ........................ 216 Report erratum this copy is (P1.0 printing, October 2008) CONTENTS 11 11 Parlez-Vous Français? Making It Multilingual 218 11.1 Offering an Application in Multiple Languages . 218 11.2 Translating the Text of an Application .......... 221 11.3 Switching Between Languages ............... 233 11.4 Using Different Resource Bundles ............ 236 III In High Gear 244 12 Completing the Stack 245 12.1 Persistence with Stripersist, JPA, and Hibernate . 245 12.2 Dependency Injection with Spring ............ 261 12.3 Automated Testing with Mock Objects .......... 267 13 Tapping into Stripes 277 13.1 Houston: Exception Handling ............... 277 13.2 Customizing URL Bindings ................ 283 13.3 Everything Is Possible: Interceptors ........... 294 13.4 Interceptor Example: Adding Support for Guice . 299 13.5 Another Interceptor Example: Ensuring Login . 302 13.6 The Stripes Life Cycle in More Detail ........... 304 14 It’s a Dangerous World: Adding Security 307 14.1 Controlling Parameter Binding .............. 307 14.2 Preventing Cross-site Scripting Attacks ......... 311 14.3 Using Encryption ...................... 313 14.4 Ensuring the User Is Logged In .............. 315 14.5 Showing Users Their Data, Not Other People’s . 319 14.6 Using Roles .......................... 321 15 Using JavaScript and Ajax 336 15.1 Using JavaScriptResolution ................ 337 15.2 Working with Ajax Requests and Responses . 341 15.3 Ajaxifying the Webmail Application ............ 351 15.4 Adding Client-Side Validation ............... 360 A Configuration Reference 369 A.1 Required Configuration ................... 369 A.2 Extensions .......................... 370 A.3 Settings ............................ 376 A.4 Interceptors ......................... 378 Report erratum this copy is (P1.0 printing, October 2008) CONTENTS 12 B Resources 379 B.1 Stripes Online Resources ................. 379 B.2 Stripes Dependencies .................... 379 B.3 Third-Party Frameworks, Libraries, and Tools . 380 B.4 Development Tools ..................... 381 B.5 Bibliography ......................... 381 Index 382 Report erratum this copy is (P1.0 printing, October 2008) Everything should be made as simple as possible, but not one bit simpler. Albert Einstein Chapter 1 Introduction Welcome to Stripes! Stripes is a framework that makes developing Java web applications easier.