Getting Started with Apache Struts
Tutorial - Apache Struts 2 - From Soup to Nuts - Part 4
Same Stuff, Different Day – Struts 2 Life Cycle
A framework is a semi-complete application. By providing a backbone structure, the framework lets developers add the code that makes this application unique, and reuse the underlying architecture again and again.
The Struts 2 architecture strives for simple elegance. When a web client sends a request to a Struts 2 application, the framework provides a seven step processing cycle
- Accept Request,
- Select Action,
- Push Interceptors,
- Invoke Action,
- Select Result,
- Pop Interceptors, and
- Return Response.
At each step in the cycle, developers can provide their own code to define how their application reacts to a particular request.
- Accept Request: First, a web browser makes a request. The request might be for a resource like a HTML page, a PDF, or (among other things) the special “Action” resource that can create a dynamic response.
- Select Action: The incoming request is transferred to a Struts 2 component called a Filter Dispatcher. Whenever this request is for a Struts Action, our dispatcher passes the request through to the rest of the framework. (Otherwise, the request just passes through our dispatcher, untouched.)
- Push Interceptors: Whenever a request enters the framework, we might want to validate input, or transfer values to an internal object, or upload a file, or any combination of similar operations. (In other words, we want to “intercept” the request to do some housekeeping before going on to the main event.) The framework Interceptor components handle operations that need to be applied to more than one request.
- Invoke Action: Aside from the Interceptor operations, we will also want to invoke an operation unique to the request (the “main event”). For example, we might want to work with a database to store or retrieve information. After a request passes through the outer layer of Interceptors, a method on an Action class can be called to handle any special processing for the request.
- Select Result: After the Action method fires, we need to tell the rest of the framework what to do next. We've handled the request, and now we need to create the response (or “result”). As aforementioned, to bootstrap the response process, the Action method returns a string token representing the outcome of the request, such as “success”, “failure”, “cancel”, or something more specific, like “logon”. The framework matches that token with the name of a Result component. The Result is responsible for handling the response, either by rendering output for the browser, or by transferring control to another resource that will render the response. Most often, “another resource” will be a JavaServer Page formatted with Struts Tags.
- Pop Interceptors: After the Result handler fires, the request passes back through the set of Interceptors. Some Interceptors work with incoming requests, some work with outgoing requests, and some work with both.
- Return Response: The request exits the framework, and the web server sends our response back to the web browser.
Architecturally, Struts 2 is an Enterprise Model View Controller framework where the Action provides the Model, the Result represents the Controller, and Tags render the View.
But wait, there’s more – Plugins
Struts 2 provides a simple plugin architecture so that developers can extend the framework just by adding a JAR to the application's classpath. Since plugins are contained in a JAR, they are easy to share with others. Here’s a sampling of the many plugins available for Struts 2.
- Hibernate: Hibernate Validator integration, Hibernate Core Sessions and Transactions injection capabilities and a Configuration Management Web Tool.
- Guice: Actions, Interceptors, and Results to be injected by Guice.
- OSGI: starting an instance of Apache Felix inside a web application, and scanning installed bundles for Struts configuration
- REST: provides tools to build RESTful applications
- Sitemesh: Sitemesh templates functionality access Struts information.
If you’re ready to take Struts 2 for a spin, the simplest thing is to fire up your favorite editor or IDE, and start coding.
To give you a staring place, the Struts 2 distribution bundles a “Struts Blank” application. It’s a ready-to-deploy WAR, designed to serve as a starting place for your applications.
The Blank application is internationalized and includes a number of example pages, to help you get started.
Of course, there are a number of online resources for learning more about Struts 2, most of which are documented on the official website at struts.apache.org. Another great resource is Struts 2 in Action by Don Brown, et al. Highly recommended!
Ted Husted is the co-author of Struts in Action (first edition), a former Struts committer, and served as Release Manager for the initial Stuts 2 distribution. When he can, Ted now helps out with the Vosao CMS project (www.vosao.org), an open source web content management system for the Google Apps Engine. Ted will be speaking at ApacheCon NA in November 2010 on the “Secret Life of Open Source” and “.NET @ Apache.org”. He works as a Business Analyst with NimbleUser, a technology company helping professional and trade associations do more with web-based applications.
This article originally appeared in Java Tech Journal - Java Web Frameworks - check out more of that issue here!