Viral Patel is a keen technology blogger and enthusiast. Working with a software company, he uses his leisure time to learn and explore emerging areas in tech world. His passion for technology has motivated him from creating an operating system to writing tutorials/articles on Java, JavaEE, Spring, Hibernate, Struts, jQuery, PHP. Visit the website http://viralpatel.net for a wide range of articles and tutorials on these topics. Viral has posted 13 posts at DZone. View Full User Profile

Struts 2 Tutorial: Introduction to Struts 2 Framework

01.13.2010
| 109417 views |
  • submit to reddit
Let us begin Part 1 of 7-parts series tutorials on Struts 2 Framework. In these tutorials we will discuss the Introduction of Struts2 framework, validation framework, the interceptors in struts 2, tiles plugin and its application with example, a file upload example and struts2 ajax example.

Struts 2 Tutorial List

  • Part 1: Introduction to Struts 2
  • Part 2: Create Hello World Application in Struts 2
  • Part 3: Struts 2 Validation Framework Tutorial with Example
  • Part 4: Struts 2 Tiles Plugin Tutorial with Example
  • Part 5: Struts 2 Interceptors Tutorial with Example
  • Part 6: Struts 2 File Upload and Save Example
  • Part 7: Struts 2 Ajax Tutorial with Example

Introduction of Struts 2 Framework

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

Apache Struts2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

Struts 2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action.

Struts2 supports annotation based configurations which are easy to create and more intuitive. Action class in Struts 2 act as the model in the web application. Unlike Struts, Struts 2 Action class are plain POJO objects thus simplifying the testing of the code. Struts2 also comes with power APIs to configure Interceptors that reduce greatly the coupling in application. The view part of Struts 2 is highly configurable and it supports different result-types such as Velocity, FreeMarker, JSP, etc.

Architecture of Struts 2

Struts 2 Architecture is based on WebWork 2 framework. It leverages the standard JEE technologies such as Java Filters, JavaBeans, ResourceBundles, Locales, XML etc in its architecture.
Following is its framework diagram.

struts 2 architecture
Image Courtesy: struts.apache.org

 

  1. The normal lifecycle of struts begins when the request is sent from client. This results invoke the servlet container which in turn is passed through standard filter chain.
  2. The FilterDispatcher filter is called which consults the ActionMapper to determine whether an Action should be invoked.
  3. If ActionMapper finds an Action to be invoked, the FilterDispatcher delegates control to ActionProxy.
  4. ActionProxy reads the configuration file such as struts.xml. ActionProxy creates an instance of ActionInvocation class and delegates the control.
  5. ActionInvocation is responsible for command pattern implementation. It invokes the Interceptors one by one (if required) and then invoke the Action.
  6. Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
  7. The Interceptors are executed again in reverse order and the response is returned to the Filter (In most cases to FilterDispatcher). And the result is then sent to the servlet container which in turns send it back to client.

Request Processing Lifecycle

 


The request processing lifecycle of Struts2 framework is pretty much discussed in above section where we saw the architecture of Struts 2 framework.

 

  1. Request is generated by user and sent to Servlet container.
  2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.
  3. One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
  4. Action is executed and the Result is generated by Action.
  5. The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.

AJAX Support in Struts 2

AJAX is a well known term in web development. It is now possible to write desktop like web2.0 application using AJAX. Untill Struts 1.x, developer had to write and maintain the code in javascript to add AJAX support.
But now Struts 2 gives you Ajax ‘out of the box’. No writing of javascript, no debugging against various browsers; just configure and go.

Struts 2 comes with highly configurable AJAX tag library which can be used directly without writing JavaScript code. Struts 2 also support Dojo library. Its now very easy to add AJAX enabled feature such as Autocomplete to your web application.
Related: Introduction to DOJO Toolkit

Comparison of Struts 1 and Struts 2

Let us see the basic difference between Struts 1 and 2 framework.

  1. Unlike Struts 1, Struts 2 does not need to implement Action class. The Action in Struts 2 is a POJO object. Thus making it easy to unit test the code.
  2. Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
  3. Struts 1 Actions have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked. Struts 2 Actions are not coupled to a container. Most often the servlet contexts are represented as simple Maps, allowing Actions to be tested in isolation.
  4. Struts 1 uses an ActionForm object to capture input. Like Actions, all ActionForms must extend a base class. Since other JavaBeans cannot be used as ActionForms, developers often create redundant classes to capture input. Struts 2 uses Action properties as input properties, eliminating the need for a second input object. Input properties may be rich object types which may have their own properties.
  5. Struts 1 integrates with JSTL, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support. Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called “Object Graph Notation Language” (OGNL).
  6. Struts 1 uses the standard JSP mechanism for binding objects into the page context for access. Struts 2 uses a “ValueStack” technology so that the taglibs can access values without coupling your view to the object type it is rendering.
  7. Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in the module must share the same lifecycle. Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.

Moving On

Now that we have idea about architecture of Struts 2 framework and its lifecycle, in the next part we will create a working Struts 2 Hello World application from scratch.

 

Original article: http://viralpatel.net/blogs/2009/12/introduction-to-struts-2-framework.html

Published at DZone with permission of its author, Viral Patel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Freddy Daoud replied on Thu, 2010/01/14 - 3:24am

Point 1 of Comparison of Struts 1 and Struts 2 is true in theory, but not in practice. Indeed, to take advantage of several core Struts 2 features, classes not only have to implement the Action interface, they have to extend the ActionSupport class.

Stephen Zhou replied on Sun, 2010/01/17 - 12:05pm

excellent tutorial!

Sendilkumar Mani replied on Thu, 2010/05/06 - 5:35am

Hi Buddy(Viral),

 

Its simply superb, Keep it up keep posting, the advanced things dude...

 

Sendil

Praveen Agrawal replied on Thu, 2010/06/03 - 10:54pm

hi friend,

This is really excellent tutorial.

 Thanks 

Mahesh Musuram replied on Tue, 2010/06/15 - 4:06am

it's really good and simple to understood

where can i found other parts

Johannes Geppert replied on Mon, 2010/09/13 - 3:21pm

the ajax part is a little out of date. because the dojo plugin is deprecated and will be removed in one of the next version. but there exists other third party plugins for AJAX support with jquery or yui.

Mayank Gupta replied on Fri, 2011/02/11 - 12:00am

very good tutorial

Srinivasarao Go... replied on Wed, 2011/11/02 - 6:48am

Very good tutorial for beginners.

Sumant Rajpathak replied on Mon, 2011/11/21 - 2:38am

gud tutorial

Alex Huang replied on Mon, 2011/12/12 - 9:29pm

good tutorial.

Sindy Loreal replied on Sat, 2012/02/25 - 8:28am

i have to integrate my product with struts. product can only call JSPs (extension .jsp). now I need to get some values in request to display when JSP getting rendered. where should i set those values?? i m using struts 2.0

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.