Struts 2 in Action cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

About This Book

published book

Welcome to Struts 2! If you’ve picked up this book, we suspect you’re a Java developer working with web applications who’s somehow or other heard about Struts 2. Perhaps you’ve worked with the Struts 1 framework in the past, perhaps you’ve worked with another framework, or perhaps this is your first step into Java web application development. Whichever path has led you here, you’re probably looking for a good introduction to the new Struts 2 framework. This book intends to give you that introduction and much more. If you’ve never heard of Struts 2, we cover the basics in enough depth to keep you in tow. If you know what Struts 2 does, but want a deeper understanding of how it does it, we’ll provide that too.

Struts 2 is a Java web application framework. As you know, the Java world is vast and a Struts 2 application may travel far and wide in this world of Java. With that said, one of the biggest challenges faced by a Struts 2 book arises from trying to determine what content to include. This book could have been three times as long if we’d taken all of the good advice we received about what to include. We apologize to those whose course of normal development takes them outside the boundaries of our content. Please believe us when we say that we agonized over what to include and what not to include.

Struts 2 is much more than a revision of the Struts 1 framework. If you hadn’t yet heard anything about Struts 2, you might expect, based upon the name, to find a new release of that proven framework. But this is not the case. Its relationship to that older framework is based in philosophy rather than in code base. Struts 1 was an action-oriented framework that implemented a Model-View-Controller (MVC) separation of concerns in its architecture. Struts 2 is a brand new implementation of those same MVC principles in an action-oriented framework. While the general lay of the land will seem familiar to Struts 1 developers, the new framework contains substantial architectural differences that serve to clean up the MVC lines and make the development process all that more efficient. We cover the new framework from the ground up, taking time to provide a true introduction to this new technology while also taking pains to give an inside view.

The organization of this book aims to walk you through Struts 2 in a sequence of increasing complexity. We start with a couple of preliminary chapters that introduce the technological context of the framework, give a high-level overview of the architecture, and present a bare-bones HelloWorld sample application to get your environment up and running. After this brief introduction, we set off into a series of chapters that cover the core concepts and components of the framework one by one. We take time to explain the functionality of each component in depth. We also provide real code examples as we begin the development of our full-featured sample application, the Struts 2 Portfolio. Finally, the later chapters provide some advanced techniques for tweaking a bit more out of the core components, as well as introducing some advanced features of the framework such as plug-ins and Spring integration. The following summarizes the contents chapter by chapter.

Roadmap

Chapter 1 gets us started gently. We begin with a quick survey of the context in which Struts 2 occurs, including short studies of web applications and frameworks. We then take the obligatory architectural look from 30,000 feet. Unless you’re familiar with WebWork, the true code base ancestor of Struts 2, this high-level overview of the framework will be your first look at a fairly new and interesting way of doing things. Some advanced readers may feel comfortable skipping this first chapter.

Chapter 2 revisits the architectural principle of the first chapter as demonstrated in a HelloWorld sample application. We do two versions of HelloWorld. First, we show how to use XML to declare your Struts 2 architectural metadata; then we do it again using Java annotations for that same purpose. The HelloWorld application both reinforces architectural concepts and gives you a skeleton Struts 2 application.

Chapter 3 kicks off the core portion of the book by introducing and thoroughly covering the Struts 2 action component. Actions are at the heart of Struts 2, and it wouldn’t make sense to start anywhere else. In addition to revealing the inner workings of this core component, we also begin to develop the full-featured Struts 2 Portfolio sample application in this chapter.

Chapter 4 continues the core topics by introducing one of the most important components of the framework, the interceptor. Struts 2 uses interceptors to implement almost all of the important functionality of the framework. We make sure you know what they are, how they work, and when you should consider implementing your own.

Chapter 5 finishes off the discussion of framework fundamentals by covering the data transfer mechanisms of the system. One of the most innovative features of Struts 2 is its automatic transfer and conversion of data between the HTTP and Java realms. Elusive but important players such as OGNL, the ValueStack, and the ActionContext are fully demystified and put to work for the average workingman developer.

Chapter 6 starts coverage of the view layer aspects of the framework. In particular, this chapter will introduce the Struts 2 Tag API. This introduction explains how to use the OGNL expression language to get your hands on the data in the ValueStack and ActionContext, which we met in the previous chapter. The chapter provides a reference to basic tags that you’ll use to pull data into your rendering view pages, as well as tags to control the flow of your rendering view pages. Finally, we wrap up the chapter by providing a primer to the OGNL expression language, which will prove useful in your daily tag development.

Chapter 7 introduces the second major chunk of the Struts 2 tags, the Struts 2 UI components. The UI components are the tags that you use to build the user interfaces of your web application. As such, they include form components, text field components, and the like. But don’t mistake the Struts 2 UI components for your father’s HTML tags, if you know what I mean.

Chapter 8 rounds out treatment of the view layer of the framework by introducing the result component. This core component highlights the flexible nature of Struts 2. With Struts 2’s highly decoupled result component, you can build results independent of the actions. After covering the basics, we show what we mean by building a JSON result that can return a JSON stream based on the data prepared by any action, regardless of whether that action knows anything about JSON. We use this example to implement some Ajax for the Struts 2 Portfolio.

Chapter 9 begins to show you how to bring your basic Struts 2 application up to industry standards. In particular, we take the opportunity to go off topic by showing you how to use Struts 2’s Spring plug-in to bring dependency injection into your application. We then up the ante by showing you how to wield that Spring integration to upgrade your application to a JPA/Hibernate persistence layer that’s managed by Spring’s wonderful support for those technologies.

Chapter 10 continues the trend of making your application more refined by showing how to use Struts 2’s validation framework to gain metadata-driven validation of your data.

Chapter 11 introduces the Struts 2 internationalization and localization support, and carefully walks you through all of the fine-grained details.

Chapter 12 introduces the Struts 2 plug-in architecture. Like any well-designed software, you should be able to extend the functionality without modifying existing code, and Struts 2 leverages the plug-in architecture for this very purpose. If you use Firefox or Eclipse, you already know how this works. The chapter explores the details and shows you how to write a plug-in from scratch.

Chapter 13 reveals best practices and tips from the trenches. This chapter presents topics that range from optimizing your development environment to registering your web features using a technique known as wildcard mappings. Of course, you will find a mishmash of useful tips in between.

Chapter 14 organizes a migration plan to help you transition from Struts 1.x to the exciting Struts Web 2.0 world. This chapter also points out similarities and differences between the two Struts versions.

Chapter 15 reveals techniques that let you leverage the true spirit of the framework. This chapter contains advanced concepts, and should be read several times before starting any large-scale Struts 2 project. It is chock-full of techniques that you’ll be happy you leveraged as you look back over your code base.

Code conventions

The following typographical conventions are used throughout the book:

  • Courier typeface is used in all code listings.
  • Courier typeface is used within text for certain code words.
  • Italics are used for emphasis and to introduce new terms.
  • Code annotations are used in place of inline comments in the code. These highlight important concepts or areas of the code. Some annotations appear with numbered bullets like this that are referenced later in the text.

Code downloads

You can download the sample code for this book via a link found on the book’s homepage on the Manning website, www.manning.com/Struts2inAction or www.manning.com/dbrown. This will get you the SampleApplication.zip archive file, which contains a couple of Java Servlet web application archive files-WAR files—as well as some documentation of the source. Instructions on how to install the application are contained in a README file in that download.

We should make a couple of points about the source code. First, all of the sample code for the book is contained in the Struts2InAction.war web application. Note that this web application uses a modularized structure to present a subapplication, if you will, for each of the chapters of the book. Throughout the book, we develop what we refer to as the Struts 2 Portfolio. This is our full-featured demonstration of a Struts 2 sample application. We develop the Struts 2 Portfolio incrementally throughout the chapters of the book. This means that the Struts2InAction.war web application contains many versions, in increasing power, of the Struts 2 Portfolio. The versions are modularized by chapter number.

Since we recognize that troubleshooting the deployment of a large application like the full Struts2InAction.war can be daunting to developers new to the platform, we’ve also provided a HelloWorld.war web application that contains only the HelloWorld portion of the larger sample application. This will help readers more quickly get a Struts 2 application up and running without the unwarranted complexity of such things as setting up a database.

Author Online

The purchase of Struts 2 in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and other users. You can access and subscribe to the forum at www.manning.com/Struts2inAction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue among individual readers and between readers and authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

Get Struts 2 in Action
add to cart
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage