Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Apache Struts - From Novice To Professional (2006)

.pdf
Скачиваний:
56
Добавлен:
17.08.2013
Размер:
11.68 Mб
Скачать

Beginning Apache Struts

From Novice to Professional

■ ■ ■

Arnold Doray

Beginning Apache Struts: From Novice to Professional

Copyright © 2006 by Arnold Doray

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN-13 (pbk): 978-1-59059-604-3

ISBN-10 (pbk): 1-59059-604-8

Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Lead Editor: Steve Anglin

Technical Reviewer: Kunal Mittal

Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore,

Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft,

Jim Sumser, Matt Wade

Project Manager: Julie M. Smith

Copy Edit Manager: Nicole LeClerc

Copy Editor: Liz Welch

Assistant Production Director: Kari Brooks-Copony

Production Editor: Katie Stence

Compositor: Susan Glinert

Proofreader: Lori Bring

Indexer: Valerie Perry

Artist: Kinetic Publishing Services, LLC

Cover Designer: Kurt Krames

Manufacturing Director: Tom Debolski

Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.

The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at http://www.apress.com in the Source Code section.

To my darling wife Lillian, for her love, encouragement, and constant support, To my mother for always believing in me,

To my father for showing me by example that you can accomplish almost anything you put your mind to,

And to the One who gave up His life for us two thousand years ago.

Contents at a Glance

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

PART 1 ■ ■ ■ Basic Struts

CHAPTER 1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 3

CHAPTER 2

Servlet and JSP Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 7

CHAPTER 3

Understanding Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

CHAPTER 4

Custom Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

CHAPTER 5

The MVC Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

CHAPTER 6

Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

CHAPTER 7

Processing Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

CHAPTER 8

Basic Struts Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

79

CHAPTER 9

Configuring Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

93

CHAPTER 10

More Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

107

CHAPTER 11

Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

129

CHAPTER 12

Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

143

CHAPTER 13

Review Lab: Editing Contacts in LILLDEP . . . . . . . . . . . . . . . . . . . . .

157

PART 2 ■ ■ ■ Advanced Struts

CHAPTER 14

Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

161

 

CHAPTER 15 The Validator Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

195

 

CHAPTER 16

Dynamic Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

221

 

CHAPTER 17

Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

239

 

CHAPTER 18 Review Lab: The Collection Facility . . . . . . . . . . . . . . . . . . . . . . . . . .

267

 

CHAPTER 19

Developing Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

277

 

CHAPTER 20 JavaServer Faces and Struts Shale . . . . . . . . . . . . . . . . . . . . . . . . . .

307

v

APPENDIX A Frameworks for the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

359

APPENDIX B Commonly Used Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

375

APPENDIX C Struts Tag Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

381

APPENDIX D Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

469

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

479

vi

Contents

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

PART 1 ■ ■ ■ Basic Struts

CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

What Is a Web Application? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Struts Can Do for You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 About You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

CHAPTER 2 Servlet and JSP Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Lab 2: Installing Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Servlet Container Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Important Servlet Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

JavaServer Pages (JSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Deconstructing Hello.jsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Final Thoughts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

CHAPTER 3 Understanding Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Lab 3: Scopes Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Session and Request Scope Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

vii

viii

C O N T E N T S

 

 

CHAPTER 4 Custom Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

Custom Tag Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

How Custom Tags Are Processed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

The Java Handler Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

The TLD File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Lab 4: A Temperature Conversion Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Step 1: Prepare the Development Environment and Scripts . . . . . . 32

Step 2: Write the Java Tag Handler . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Step 3: Writing the Tag Library Descriptor file . . . . . . . . . . . . . . . . . 33

Step 4: Amend web.xml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Step 5: Write Your JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Step 6: Deploy and Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Professional Java Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

CHAPTER 5 The MVC Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

The Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Requirement 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Requirement 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Requirement 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Requirement 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Requirement 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Lab 5: MVC Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Which Comes First? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Struts and MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Lifecycle of a Struts Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Frameworks for the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

CHAPTER 6 Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Processing Simple Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Anatomy of ActionForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Using ActionErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

C O N T E N T S

ix

Lab 6: ContactForm for LILLDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Step 1: Prepare the Development Environment and Scripts . . . . . . 62

Step 2: Implement Getters and Setters for ContactForm . . . . . . . . . 63

Step 3: Implement validate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Step 4: Implement reset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

What Has Struts Done for You Today? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

CHAPTER 7 Processing Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

1,2,3 Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 The Statelessness of Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Subclassing Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Business Logic in the Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . 70

Complex Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Data Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Lab 7: Implementing ContactAction for LILLDEP . . . . . . . . . . . . . . . . . . . . 76

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

CHAPTER 8 Basic Struts Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

Page Processing Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Evaluation, Replacement, and Sending . . . . . . . . . . . . . . . . . . . . . . . 81

The View Component of the Registration Webapp . . . . . . . . . . . . . . . . . . 82

Declaring and Installing the HTML and Bean Libraries. . . . . . . . . . . 83

Displaying Static Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

Forms and Form Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Data Input Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Displaying Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

Synopsis of HTML and Bean Tag Libraries . . . . . . . . . . . . . . . . . . . . . . . . 88

Lab 8: Contact Entry Page for LILLDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

CHAPTER 9 Configuring Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

The Structure of struts-config.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Configuring the Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Declaring Form Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Declaring Global Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

Declaring Global Forwards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97