Table Of ContentApache Struts 2
Web Application Development
Design, develop, test, and deploy your web applications
using the Struts 2 framework
Dave Newton
BIRMINGHAM - MUMBAI
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Apache Struts 2 Web Application Development
Copyright © 2009 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the author, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged
to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2009
Production Reference: 2040609
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847193-39-1
www.packtpub.com
Cover Image by Vinayak Chittar (vinayak.chittar@gmail.com)
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Credits
Author Indexer
Dave Newton Monica Ajmera
Reviewers Editorial Team Leader
Sandeep Jadav Akshara Aware
Dale Newfield
Michael Minella Project Team Leader
Lata Basantani
Sharad Sharma
Acquisition Editor Project Coordinator
Viraj Joshi Abhijeet Deobhakta
Development Editor Proofreader
Ved Prakash Jha Joel T. Johnson
Technical Editor Production Coordinators
Gaurav Datar Shantanu Zagade
Adline Swetha Jesuthas
Copy Editors
Sumathi Sridhar Cover Work
Shantanu Zagade
Leonard D'Silva
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
About the author
Dave has been programming for as long as he can remember, and probably a bit
longer than that. He began by playing a Star Trek game on a library computer, and
quickly graduated to educating his grade school teachers about computers and the
BASIC language. Receiving a TRS-80 Model 1 was pretty much the end of his social
life, and he's never looked back.
A diverse background in both programming languages and problem spaces has
provided him with a wide range of experience. Using a diverse set of languages,
which includes Pascal, Forth, Fortran, Lisp, Smalltalk, assembly, C/C++, Java,
and Ruby, brings an appreciation for many programming paradigms. Working
on AI, embedded systems, device drivers, and both desktop and web application
programming, has brought him an appreciation for the problems and solutions that
each environment offers.
Even after thirty years of typing in his first infinite loop, he's still entertained by the
process of programming. He never tires of looking for ways to do less work, and
is convinced that he should never have to repeat himself to a computer. He still
occasionally writes an infinite loop.
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Acknowledgement
I'd like to thank Packt for providing me an opportunity to share what I hope is some
useful information about my favorite Java web application framework, Struts 2, and
along the way, throw in some techniques I've found my clients appreciate (and many
of them mean I get to do less work, so it's a win-win!)
My parents, of course, deserve far more thanks than I could ever hope to express.
From them, I received a robust sense of exploration and curiosity, which are traits
that have served me well during my personal and professional career. They, and the
rest of my family, have prodded me along by continually asking, "How's the book
coming?", which was surprisingly motivating. They have also provided, along with
my friends, much needed moral support during recent months.
I'd like to thank my co-workers, who have zero compunction about telling me when
I'm completely wrong, and who provide me with a constant stream of ideas about
programming, the associated processes, and how to make programming easier and
more flexible.
The creators of the frameworks and libraries we use every day deserve more than
they usually receive. Putting one's code out there for everyone to see and use is
a brave step. They're under-appreciated when everything works, and unfairly
punished when it doesn't. I hope this book pays homage to them in some small way,
and contributes back to the various communities that have made my programming
work easier.
Finally, the open source and Java communities deserve a hearty "Huzzah!" It
includes places like the Struts 2 mailing lists, where all types of developers contribute
by asking and answering questions, making suggestions, and politely reminding
those involved with the framework that our documentation could be better. It also
includes JavaRanch, where "No question is too simple", and sometimes the "simplest"
questions turn out to be surprisingly interesting. Finally, the Apache Foundation
and fellow Struts developers, who have made Struts possible in all its incarnations.
All those people who write the code that we use, contribute to discussions, and try
to help others out know who they are. I can't possibly begin to list all of them,
but thanks.
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
About the reviewers
Sandeep Jadav has been in the IT industry for three years and is currently working
as a Software Engineer for an IT firm. Sandeep is an MCA qualified professional
and is well-versed with Java technologies. He empowers people around him to work
and play better, by helping them resolve complex issues with the simplest and most
durable solutions.
In addition to reviewing, Sandeep has a history of using his technical skills for more
practical purposes—providing technical leadership to past companies. He has an
experience in developing on a large scale, n-tier and Web applications, for business
and engineering uses.
Sandeep has a large network of friends and makes frequent contributions to a variety
of content areas in many online IT communities.
I would first like to thank Packt Publishing, and the author Dave
Newton, for spearheading this edition of Apache Struts 2 Web
Application Development, and giving me an opportunity to revisit
and improve upon the first efforts.
I am deeply grateful to my family for allowing me to encroach on
many months of what should have been my family's quality time.
I extend my deepest appreciation to my friends, for all their support,
encouragement, and guidance throughout my work.
I appreciate Packt Publishing for allowing me to gain a very new and
delighting experience of reviewing the book.
While reviewing may seem a solitary pursuit, it is actually very
much a collaborative effort, and as such, I extend my thanks and
appreciation to my author, editor, and the staff at Packt Publishing
for their support throughout this project.
Finally, I especially thank Payal for providing great support on each
and every step of my life.
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Dale Newfield is a computer scientist trained at Carnegie Mellon and the University
of Virginia (ABD). Mr. Newfield has designed, built, and maintained a wide variety
of systems in many languages. He has vertical knowledge in fields as disparate as
graphics, user interfaces, virtual environments, networking, network technology
design, network modeling, distributed and disconnected computation, bioinformatics,
along with both web and POS systems requiring tight integration of hardware and
software. Having built scalable web applications using both Struts1 and Struts2, his
input was helpful in keeping this text focused on teaching best practices.
Michael T Minella has been working with, and teaching about, open source
software and agile methodologies for over seven years. He holds degrees from
Northern Illinois University and DePaul University in Computer Science and
E-Commerce Technologies respectively.
Michael lives just outside Chicago, IL, and works at a major financial exchange
there. In addition to his day job, Michael currently teaches at DeVry University,
has authored a Refcard on JUnit and EasyMock (http://refcardz.dzone.
com/refcardz/junit-and-easymock), and maintains the site http://www.
michaelminella.com.
Michael would like to thank his wife Erica for her continued support
in all the ways he expands his career.
Sharad Sharma is working as a Software Engineer with a reputed MNC. He
completed his Bachelors in Technology (B.Tech) from Sikkim Manipal University,
Sikkim, and has a passion to learn and teach new technologies. He has successfully
completed many projects based on Java/J2EE technology. In spite of having less
experience, due to his dedication and hard work, he was able to achieve the top
position among all the developers of the organization. This is the first book he has
worked upon and wishes to work on many more in future.
I would like to thank my family and friends for the support they
have provided me in all the areas.
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Table of Contents
Preface 1
Chapter 1: Struts and Agile Development 7
Struts 2 in a nutshell 7
The filter dispatcher 8
Interceptors 8
Actions 9
Results 9
Plug-ins 10
Agile in a nutshell 10
User stories 11
Testing 11
Refactoring 11
Short iterations 11
Real applications in a nutshell 12
Making it pretty 12
JavaScript 13
Documentation 13
All of the rest 13
Getting started 13
Creating our own applications 14
Doing it "by hand" 14
Using Maven 14
Summary 15
Chapter 2: Basic Configuration 17
Setting up our environment 17
A sanity-checking application 18
Configuring web.xml for Struts 2 19
Writing our first action 20
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Table of Contents
Configuring our first action with XML 20
Configuring our result 21
Choosing an action method 22
Getting started with our application 23
Gathering user stories—defining our application 23
Building skeletal applications using wildcards 24
Matching multiple wildcards 25
More wildcard tricks 25
Packages and namespaces 26
Creating site navigation 26
Including external configuration files 28
Our application so far 28
Examining our configuration 29
Configuration via convention and annotations 30
The Convention Plug-in and action-less actions 30
The Convention Plug-in and action configuration 31
Configuring the Convention Plug-in with annotations 32
Summary 34
Chapter 3: Actions and ActionSupport 35
ActionSupport and its interfaces 35
The Action interface 36
Action's convenience strings 36
The TextProvider interface 36
Detour—action properties, JSPs, and more tags 38
Continuing with message lookup 41
Parameterized messages 42
The LocaleProvider interface 44
The Validateable and ValidationAware interfaces 44
Implementing our first user story 45
Refining our story 45
Creating the recipe form 46
Adding some validation 48
Displaying our error messages 49
More action interfaces 49
Detour—creating the list of interfaces to explore 50
Leveraging the IDE 50
Using the command line 51
Examining class files 52
Additional action interfaces 53
Preparable interface 53
Accessing scoped attributes (and request parameters) 53
[ ii ]
This material is copyright and is licensed for the sole use by Paul Corcorran on 5th July 2009
8601 ave. p #1, , lubbock, , 79423
Description:This book takes a clear approach, focusing on one topic per chapter, but interspersing other issues in the mainline text and in chapter detours. Taking a practical approach, it discusses agile web development using Struts 2, with plenty of examples for better understanding. This book is for Java dev