WebWork in Action Licensed to Shirong Chen <[email protected]> Licensed to Shirong Chen <[email protected]> WebWork in Action PATRICK LIGHTBODY JASON CARREIRA MANNING Greenwich (74° w. long.) Licensed to Shirong Chen <[email protected]> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax:(203) 661-9018 Greenwich, CT 06830 email: [email protected] ©2006 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Tiffany Taylor 209 Bruce Park Avenue Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-394932-53-2 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 10 09 08 07 06 05 Licensed to Shirong Chen <[email protected]> brief contents PART 1 INTRODUCTION TO WEBWORK ................... 1 Chapter 1 ■ An overview of WebWork 3 Chapter 2 ■ HelloWorld, the WebWork way 19 Chapter 3 ■ Setting up WebWork 38 PART 2 CORE CONCEPTS....................................... 75 Chapter 4 ■ Implementing WebWork actions 77 Chapter 5 ■ Adding functionality with interceptors 112 Chapter 6 ■ Inversion of Control 137 PART 3 DISPLAYING CONTENT ............................ 175 Chapter 7 ■ Using results 177 Chapter 8 ■ Getting data with the expression language 209 Chapter 9 ■ Tag libraries 230 Chapter 10 ■ Velocity 254 Chapter 11 ■ UI components 271 v Licensed to Shirong Chen <[email protected]> vi BRIEF CONTENTS PART 4 ADVANCED TOPICS.................................. 311 Chapter 12 ■ Type conversion 313 Chapter 13 ■ Validating form data 333 Chapter 14 ■ Internationalization 360 Chapter 15 ■ Best practices 384 Appendix ■ WebWork architecture 424 Licensed to Shirong Chen <[email protected]> contents foreword xv preface xvii acknowledgments xix about this book xxi a look at the future xxvi about the title xxvii about the cover illustration xxviii PART 1 INTRODUCTION TO WEBWORK .........................1 1 An overview of WebWork 3 1.1 Why MVC is important 5 Classic MVC becomes outdated 6 Classic MVC gets an update: ■ the Front Controller 7 MVC evolves: the Page Controller 7 ■ 1.2 Understanding frameworks and containers 9 What is a framework? 9 What a container can do 11 ■ 1.3 WebWork: past, present, and future 13 The history of WebWork 13 Understanding the XWork core 13 ■ Future directions 15 vii Licensed to Shirong Chen <[email protected]> viii CONTENTS 1.4 The CaveatEmptor application 15 How CaveatEmptor is organized 16 1.5 Summary 17 2 HelloWorld, the WebWork way 19 2.1 Downloading WebWork 20 2.2 Preparing the skeleton 20 Creating the web.xml deployment file 21 Creating the xwork.xml ■ configuration file 23 Creating the webwork.properties ■ configuration file 23 Tips for developing WebWork apps 24 ■ 2.3 Your first action 24 Saying hello, the WebWork way 25 Displaying output to ■ the web browser 26 Configuring your new action 27 ■ 2.4 Dealing with inputs 28 2.5 Advanced control flow 31 2.6 Letting WebWork do the work 33 Taking advantage of ActionSupport 34 Intermediate modifications ■ to the JSP 35 Exploring the UI tag library 36 ■ 2.7 Summary 37 3 Setting up WebWork 38 3.1 Configuring actions, results, and interceptors 39 Overview of terminology 39 Actions 40 ■ Results 46 Interceptors 48 ■ 3.2 Advanced configuration 52 The xwork.xml DTD 52 Namespaces and packages 53 ■ Componentization using the include tag 57 3.3 Other configuration files 66 Web-app configuration: web.xml 66 Feature configuration: webwork.properties 67 3.4 Setting up your web app 70 General layout 70 Required libraries 71 Optional libraries 72 ■ ■ 3.5 Summary 72 Licensed to Shirong Chen <[email protected]> CONTENTS ix PART 2 CORE CONCEPTS ............................................ 75 4 Implementing WebWork actions 77 4.1 The Action interface 78 Result codes 78 Handling exceptions 79 ■ 4.2 Using the ActionSupport base class 80 4.3 Understanding basic validation 80 Validating an action: Validateable 81 Displaying error messages: ValidationAware 82 4.4 Using localized message texts 86 Retrieving the user’s locale: LocaleProvider 86 Displaying the localized text: TextProvider 86 Providing messages for other languages 89 4.5 Advanced inputs 90 Intermediary objects 90 Using domain objects directly 91 ■ 4.6 Working with ModelDriven actions 95 Implementing ModelDriven actions 96 Considerations when using ModelDriven 100 4.7 Accessing data through the ActionContext 102 CaveatEmptor: accessing the session 102 Example: accessing the request and response 105 4.8 Handling file uploads 107 Accessing uploaded files through the request wrapper 107 Automating file uploads 109 Configuration settings 110 ■ 4.9 Summary 111 5 Adding functionality with interceptors 112 5.1 How interceptors are called 113 5.2 Using the prepackaged interceptors 114 Utility interceptors 117 Setting parameters 119 ■ Defining workflow 123 5.3 Using prepackaged interceptor stacks 126 Licensed to Shirong Chen <[email protected]>
Description: