Starting Struts2 Ian Roughley FREE ONLINE EDITION (Non-Printable Free Online Version)

Starting Struts2 Ian Roughley FREE ONLINE EDITION (Non-Printable Free Online Version)

Starting Struts2 Ian Roughley FREE ONLINE EDITION (non-printable free online version) If you like the book, please support the author and InfoQ by purchasing the printed book: http://www.lulu.com/content/813300 (only $22.95 ) Brought to you Courtesy of This book is distributed for free on InfoQ.com, if you have received this book from any other source then please support the author and the publisher by registering on InfoQ.com. Visit the homepage for this book at: http://infoq.com/minibooks/starting-struts2 Starting Struts2 Written By: Ian Roughley © 2006 C4Media Inc All rights reserved. C4Media, Publisher of InfoQ.com. This book is part of the InfoQ Enterprise Software Development series of books. For information or ordering of this or other InfoQ books, please contact [email protected]. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recoding, scanning or otherwise except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher. Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where C4Media Inc. is aware of a claim, the product names appear in initial Capital or ALL CAPITAL LETTERS. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration. Managing Editor: Diana Plesa Cover art: Dixie Press Composition: Dixie Press Library of Congress Cataloguing-in-Publication Data: ISBN: 978-1-4303-2033-3 Printed in the United States of America Acknowledgements This book would never have been possible without the tireless effort of all the developers of WebWork, XWork and Struts2. For my transition from open source user to open source developer, I would like to thank Patrick Lightbody and Jason Carreira. To the technical reviewers – Don Brown, Philip Luppens and Rene Gielen – many thanks for providing the final adjustments on a moving target. I would like to acknowledge and thank both Jim Krygowski and James Walker for taking time out of their busy schedules to provide an impartial judgment on the continuity and content from a non-Struts2 perspective. With their assistance, the readers experience has improved without a doubt. I would like to thank Floyd Marinescu for his confidence, and for providing writing opportunities in both online and published formats. I would also like to thank my remarkable wife LeAnn (a.k.a. STR Worldwide). Her continuing support and ongoing review and non-geek analysis of the manuscript has been invaluable. iii Contents INTRODUCTION 1 WHERE STRUTS2 FITS INTO THE WEB PARADIGM 5 Servlets 6 JSP and Scriptlet Development 6 Action-Based Frameworks 7 Component-Based Frameworks 7 The Great Equalizer – Ajax 8 CORE COMPONENTS 11 Configuration 12 Actions 19 Interceptors 25 Value Stack / OGNL 29 Result Types 31 Results / View Technologies 33 ARCHITECTURAL GOALS 37 Separation of Concerns 37 Loose Coupling 39 Testability 40 Modularization 44 Convention over Configuration 47 PRODUCTIVITY TIPS 49 Re-Using Action Configurations 50 Use Pattern Matching Wildcards in Configurations 51 Utilize Alternate URI Mapping Schemes 52 Know Interceptor Functionality 55 Use Provided Interceptor Stacks 58 Take Advantage of Result Types 60 Utilize Data Conversion 61 Utilize Tabular Data Entry Support 63 Expose Domain Models in the Action 65 Use Declarative Validation Where Possible 66 Move CRUD Operations into the same Action 70 Use Annotation Where Possible 73 Options for View Technologies 79 Know the Provided Tag Libraries and their Features 81 Customize UI Themes 87 Use Global Results for Common Outcomes 89 Manage Exception Handling Declaratively 89 Internationalization 92 INTEGRATING WITH OTHER TECHNOLOGIES 97 Page Decoration and Layout 98 Business Services / Dependency Injection 100 Databases 103 Security 104 Ajax 107 ABOUT THE AUTHOR 109 END NOTES 111 Free Online Version. Support this work, buy the print copy: http://infoq.com/minibooks/starting- struts2 1 Introduction Developing web application in Java has come a long way since the first servlet specification was released in 1997. Along the way we have learned a lot and, more than a few times, we’ve improved the ways we develop web applications. Apache Struts was one of those times that we made a significant stride beyond what was currently available. Apache Struts was launched in May 2000 by Craig McClanahan, with version 1.0 officially released in July 2001. Technically it was an evolutionary step forward in web development but, more importantly, it came at the right time. Web development had been around long enough for many large projects to be built and enter maintenance phases, and for lessons to have been learned about re-usability and maintenance. Adding to this heightened need for a better solution for web application development was the “dot com boom” – as Apache Struts came on the scene in 2000, the number of web projects was dramatically increasing and it looked like there was no end in sight. The project was a welcome solution and become the de facto standard for web development for several years. Struts2 i is the next generation of Apache Struts. The original proposal, Struts Ti, was born out of a need to evolve Struts in a direction that the code base did not easily lend itself to. Around the time of that proposal, there was a movement by Patrick Lightbody to bring together leaders on several different web frameworks with the goal of achieving a common framework. Although the movement lost momentum, a commonality between WebWork and the goals of Struts Ti at the technology 1 2 | STARTING STRUTS 2 and committer level was found, and the projects were merged with WebWork providing the base technology ii . When we speak about WebWork we are really referencing two projects – XWork and WebWork. XWork is a generic command framework. It provides many of the core features such as actions, validation and interceptors, and is completely execution context independent. XWork also provides an internal dependency inject mechanism that is used for configuration and factory implementation management. WebWork, on the other hand, is a completely context dependent. It provides a wrapper around XWork with the context that is needed when working on web applications, along with specific implementations that make web developer easier . The goal of Struts2 is simple – to make web development easier for the developer. To achieve this goal Struts2 provides features to reduce XML configuration via intelligent defaults, utilizes annotations and provides conventions over configuration. Actions are now POJOs which increases testability and reduces coupling in the framework, and HTML form field data is converted to proper types for the action to use. Still further decreasing coupling is request processing has been made more modular by allowing a series of interceptors (custom or Struts2 provided) to provide pre-processing and post-processing functionality. Modularity is a common theme – a plug-in mechanism provides a way to augment the framework; key classes with the framework can be replaced with custom implementations to provide advanced features not provided out of the box; tags can utilize a variety of different rendering themes (including custom themes); and there are many different result types available for performing after-action execution tasks which include, but are not limited to, rendering JSPs, Velocity and Freemarker templates. And finally, dependency injection is now a first class citizen – provided via the Spring Framework plug-in with an option for using Plexus, and work underway for PicoContainer. INTRODUCTION | 3 My goal with this book is to familiarize you with the Struts2 framework and provide you with a solid understanding of the components that make up the framework and the configuration options that are available. I will also introduce some ways to increase your productivity – including default configurations and implementation features to be aware of; different configuration options that are available; and development techniques. We will wrap-up with a discussion of various 3 rd party integrations. This is not a comprehensive guide to all the features of Struts2. Being a new project, Struts2 is constantly evolving with ongoing changes, updates and new features. I urge you to take some time and visit the projects home page to discover options and features not covered in this book. This book refers to Struts2 version 2.0.6. Free Online Version. Support this work, buy the print copy: http://infoq.com/minibooks/starting- struts2 2 Where Struts2 fits into the Web Paradigm There are many different web frameworks available for today’s developer. Some of these come from Open Source communities, some from commercial companies, and yet others are internally developed for the current web development needs. There are over 40 iii open source frameworks alone and, although this is a large number, there are probably as many again (if not significantly more) internally built frameworks deployed in production environments. With so many choices out there, why choose Struts2? Here are some of the features that may lead you to consider Struts2: Action based framework Mature with a vibrant developer and user community Annotation and XML configuration options POJO-based actions that are easy to test Spring, SiteMesh and Tiles integration OGNL expression language integration Themes based tag libraries and Ajax tags Multiple view options (JSP, Freemarker, Velocity and XSLT) Plug-ins to extend and modify framework features Of all the decisions in choosing a framework, choosing the style of framework is going to be the most controversial. Let’s take a look at how we got to today’s web application options, and where Struts2 fits into the picture. 5 6 | STARTING STRUTS 2 Servlets Servlets provided the first Java-based foray in to web development.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    122 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us