Introduction to Struts 2 Framework

Lot of times I have been asked by users on this site to write tutorial about Struts 2 Framework. My previous tutorial on Creating Struts Application in Eclipse is one of the most viewed article on this site.

So lets 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.


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:

  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.

Get our Articles via Email. Enter your email address.

You may also like...


  1. alkesh says:

    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

    • Hi Alkesh,
      I am not sure if I understand your requirement but I assume you want to set some request parameters from Struts2 Action class and need to access it from JSP(?). If this is the case, you may want to implement ServletRequestAware class in your Action class. This interface comes with a method public void setServletRequest(HttpServletRequest request); which is called by Struts2 framework. Thus it will give you access to request object.

      import org.apache.struts2.interceptor.ServletRequestAware
      public class MyRequest implements ServletRequestAware {
      	private HttpServletRequest request;
      	public String execute() {
      		//My execute method
      		request.setAttribute("someattribute", "value")
      	public void setServletRequest(HttpServletRequest request) {
      		this.request = request;		

      Hope this will solve the problem.

  2. Soma says:

    Hi – Small correction on Comparison of Struts 1 and Struts 2, point# 5 -> OGNL stands for “Object Graph Navigation Language” not “Object Graph Notation Language”

    Really i like your posts… every day i will learn some new things…Good Job..


  3. Amol says:

    Hi Viral,

    How do I use Log4j with struts 2.?

    Many thanks in advance.

  4. Ronald says:

    @ Amol

    Log4j is independent of the struts implementation.

    In a web context u can use a start servlet to intiliaze the log4j system.

    The start servlet primarily can be configured in the deployment descriptor (WEB.xml)


  5. alok says:

    I am getting “WARNING: No configuration found for the specified action: ‘login.action’ in namespace: ‘/’. Form action defaulting to ‘action’ attribute’s literal value.
    I am unable to resolve this. P;ease suggest..

    • sandip says:

      Make Sure dat yhu copied struts lib files in to Lib folder.

  6. Tebogo says:

    i would like to know how i can send a value from a bean to a jsp. here is my scenario…

    i have jsp1 which check if a person’s username and id are correct and logs them in and it compares the input text filed with a getMethod who’s setMethod was set by a result set from the database. if the set method get mehtod is equal to the username and idNum input fields then the person can be logged-in

    on jsp2 the person’s address,surname must be viewed because the login details where correct , meaning that the if statement has validates that this person’s data exist in the database…

    if( bean.getId == request.getParameter(“idNum”) && request.getParameter(“username”))
    forward my jsp2 and display data at this person’s detail on jsp2.

  7. Suresh says:

    Is it possible to concatenate two values in value attribute?…. See my sample declaration( value=”%{#dynamicRowArrayList.txtField#conut}” ) count is a name and dynamicRowArrayList is list iterator var value . Do you have any idea. please let me know.

  8. Manish Kumar says:

    I have struts 2 Simple Login Form but i have an error resource is not avalilable.
    Can u help me to figure out this problem….

  9. deva says:

    i have an property file for each action class,wr i want to put that files,,whether in my java src or in webinf classes..

  10. neha Padwal says:

    public class RegisterDAOImpl extends JdbcDaoSupport
    	private Register register;
    	public RegisterDAOImpl(){
    //	PreparedStatement preparedStatement = getConnection().prepareStatement("insert into neha.emp values('1vivk1','1vifk1','1vdik1','v1ik1','b1f')");
    	PreparedStatement preparedStatement = getConnection().prepareStatement("insert into neha.emp values(?,?,?,?,?)");
    	String userName=register.getUserName();
    	String password=register.getPassword();
    	String address=register.getAddress();
    	String city=register.getCity();
    	String email=register.getEmail();
    	preparedStatement.setString(1, userName);
    	preparedStatement.setString(2, password);
    	preparedStatement.setString(3, address);
    	preparedStatement.setString(4, city);
    	int i=preparedStatement.executeUpdate();
    	System.out.println("No of rows Updated ->" +i);
    	catch (SQLException e) {
    //Giving NullPointerException When i am fetching data from TextField nd storing it in MySql /
    //DB table using Struts Framework

    • Aj says:

      Dont use register directly instead create new instance of the register and then try using hope it will solve ur problem
      Register register = new Register();
      and then
      String userName=register.getUserName();
      String password=register.getPassword();
      String address=register.getAddress();

  11. neha Padwal says:

    Giving NullPointerException When i am fetching data from TextField nd storing it in MySql DB table using Struts Framework

  12. vikash raj says:

    i am, in a trouble while executing my simple login application with struts2.x. Actually i have written all the xml files correctly, even though my application status on server showing false. why.? what’s problem with it. Leave the clarification as soon as possible..
    Thanks in advance..

  13. Hi Viral, how are you bro? :D

    I’m here just to say that this tutorial is awesome!
    It’s so awesome that I translated it to portuguese and posted in my blog,
    with your credits obviously :D

    See ya :D

  14. Bhavika says:

    Really very gud!! i get to learn alot being a fresher :) Thanks u :)

  15. cherry says:

    Really very nice your tutorials. Thank you very much.

  16. Sumanth says:

    Hi Viral , I’m doing one small application in struts … I’ve a databse of employee where its attributes are Name,EmpID,Salary,DeptId,ZipCode…
    In my JSP user can enter EMPID and ZipCode which intern will call my stored procedure and it should give me all other details of employee… I’m stuck up in between … Please share some knowledge

  17. Pravin says:

    nice tutorial, worth to read. thank you

  18. spell says:


  19. saeed says:

    Why is not article in PDF format for download ?

  20. san says:

    I have seen Struts2 Architecture on Apache wiki but still had some gaps and would like to see a detailed explanation on that. I found it finally on your post. I didn’t see any other sources explained better than here. This post should have been in PDF/downloadable format.

Leave a Reply

Your email address will not be published. Required fields are marked *