Apache Struts 2 Web Application Development
Struts 2.1 is a modern, extensible, agile web application framework, which is suitable for both small- and large-scale web applications.
The book begins with a comprehensive look at the basics of Struts 2.1, interspersed with detours into more advanced development topics. You’ll learn about configuring Struts 2.1 actions, results, and interceptors via both XML and Java annotations. You’ll get an introduction to most of the Struts 2.1 custom tags, and also learn how they can assist in rapid application prototyping and development.
From there, you’ll make your way into Struts 2.1’s strong support for form validation and type conversion, which allows you to treat your form values as domain objects without cluttering your code. A look at Struts 2.1’s interceptors is the final piece of the Struts 2.1 puzzle, which allows you to leverage the standard Struts 2 interceptors, as well as implement your own custom behavior.
The book closes with a look at some tools that make the application development life cycle easier to manage, particularly in a team environment, and more automatic.
What This Book Covers
Chapter 1 gives us a bird’s-eye view of Struts 2 and examines some useful techniques of lightweight, agile development.
Chapter 2 gives an introduction to Struts 2 application configuration, using both XML and annotations. It also covers the beginning of our sample application, RecipeBox.
Chapter 3 covers some of the functionality provided by Struts 2’s ActionSupport class, including I18N, and a first look at form validation. It also covers some basic RecipeBox functionality after gathering some user stories.
Chapter 4 examines several common, standard Struts 2 result types. It also covers how to write our own custom result types.
Chapter 5 gives an in-depth look at the generic Struts 2 custom tags. These include tags for iteration, list generation, conditionals, and internationalization (I18N).
Chapter 6 continues our exploration of Struts 2 custom tags, focusing especially on its form tags.
Chapter 7 examines Struts 2 form validation, including both XML and annotation-driven validation. It also teaches more about how Struts 2 converts our form values into domain objects, and shows how to create our own type converters to handle custom data types.
Chapter 8 finishes our comprehensive introduction to Struts 2, by checking out the included Struts 2 interceptors. It also discusses how to write and configure our own interceptors.
Chapter 9 looks at how to handle errors in Struts 2, as well as discusses error and exception handling in general. It also covers some general Java logging topics, focusing on using Apache Commons Logging and Log4J.
Chapter 12 covers Struts 2 themes and templates. The themes and templates in Struts 2 allow for application-wide functionality on the client side, keeping our JSP pages lightweight and adaptable. Rather than writing boilerplate HTML on our pages, we can separate it into themes and templates.
Chapter 13 takes a look at some of Struts 2’s built-in support for Ajax using the Dojo tags. It also covers the Struts 2 REST plug-in that furthers our “convention over configuration” path.
Chapter 14 covers how to apply the TDD concepts to several testing aspects, including unit, functional, and acceptance tests.
Chapter 15 looks at many aspects of documentation, including “self-documenting” code, Javadocs, generators, methodologies, and so on, with a focus on automating as much documentation as possible.
Documenting our Application
Every developer’s favorite task is documenting their application (or so I’ve heard). As irritating as documentation can be, delivering a complete solution implies comprehensive, usable documentation. This goes beyond (but includes) typical Javadocs. However, more is required in order to understand how a particular application works, how its parts fit together, where dependencies lie, and so on. Even us, the developers, benefit from having a wide variety of documentation available.
In this chapter, we’ll look at the ways in which we can document our applications, coding styles that can aid in understanding, tools and techniques for creating documentation from application artifacts, different types of documentation for different parties, and so on.
Everybody knows the basics of documenting Java, so we won’t go into much detail. We’ll talk a bit about ways of writing code whose intention is clear, mention some Javadoc tricks we can use, and highlight some tools that can help keep our code clean. Clean code is one of the most important ways we can document our application. Anything we can do to increase readability will reduce confusion later (including our own).
We’ve all heard the myth of self-documenting code. In theory, code is always clear enough to be easily understood. In reality, this isn’t always the case. However, we should try to write code that is as self-documenting as possible.
Keeping non-code artifacts in sync with the actual code is difficult. The only artifact that survives a project is the executable, which is created from code, not comments. This is one of the reasons for writing self-documenting code. (Well, annotations, XDoclet, and so on, make that somewhat less true. You know what I mean.)
There are little things we can do throughout our code to make our code read as much like our intent as possible and make extraneous comments just that: extraneous.
Document why, not what
Over-commenting wastes everybody’s time. Time is wasted in writing a comment, reading it, keeping that comment in sync with the code, and, most importantly, a lot of time is wasted when a comment is not accurate.
Ever seen this?