Basic Concept of Actions in Struts2 | ActionSupport in Struts2

October 3rd, 2019 . 5 minutes read
Blog featured image

In this technical era, a lot of frameworks are available in various different programming languages for web development as per security aspects. Here, in this blog, I am sharing a bit of information about the framework of Java technology most commonly used in modern applications i.e. “Struts2”.

Brief introduction of Struts2

Struts 2 is a flexible framework for developing Java enterprise applications. This framework was initially created by the programmer namely Craig R. McClanahan. In May 2000, Struts was taken over by the Apache Software Foundation. Struts 2 is a well-designed and organized framework. It is based on MVC architecture.

MVC Architecture

  • M stands for Model which represents database
  • V stands for View which represents the designing code for UI.
  • C stands for the Controller which is used to update models and views accordingly.

Struts2 has the capability to well-organize the Servlet and JSP based applications.  According to the official sources, Apache released the latest version of struts 2 i.e. 2.5.20, on 14 January 2019.

Basic Execution Steps of Struts 2:

Before we learn about the Action, it is a must to know about the basic execution flow of Strut2. The steps from Request to Response are as follows-

  • Request: The Client makes a request.
  • Servlet Filter: It is specified in the deployment descriptor file (web.xml) as bootstrap a component. It is a Filter Dispatcher that looks into the request and according to the URL mapping, forward the request to the mapped Action class.
  • Struts configuration (struts.xml): The request first goes to the interceptor stack before going to Action class. In this file, listed interceptors must be processed before Action class gets executed.
  • Action Class: After the interceptor gets processed, the request object is passed to Action Class. Action Class then executes the respective method for execution and it returns the result code to the Controller. There are different types of values for response code to the controller such as SUCCESS/FAILURE/ERROR etc.
  • Result: When the method of action class returns the result, the Controller selects the appropriate view to be rendered.
  • Interceptors Stack in configuration (struts.xml): Before responding back to the client, interceptors stack runs again.
  • Response: The finally selected view will be rendered as a response to the client.
execute flow of struts2
Execute Flow of Struts2

Actions in struts2

In a Struts2 application, all of the requests are processed by actions. Action is the core feature of struts2. Action is nothing but a POJO (Plain Old Java Object) class. This action class doesn’t force you to implement any other interface or extends any class. You only need to implement the execute () method which returns a string by which the result page should return, and even can define your own method for any request in this Action class. In other words, Action is the heart of struts2.

There are below steps will elaborate to create a custom Action method in action class for a specific request-

Define the Action –

In the configuration file struts.xml, we need to configure the action class using an action tag and class attribute. Define which result page should return to the user with the result tag and assign a name of the action which can be used to access this action class with the name attribute.

  • In the above snippet, the first action has been defined as the BlogAction without method attribute, thus the default method execute() will be called.
  • While the second action has been defined as the BlogAction with method attribute having value as the name of custom method i.e. SomeOtherMethod, thus the SomeOtherMethod () method will be called.

Both actions can be accessed using the following URL:

  1. http://localhost:8080/Structs2ActionsDemo/ Blogs/ latest-blog
  2. http://localhost:8080/Structs2ActionsDemo/ Blogs/ all-blogs

Struts2 has a rich set of libraries, among these some convenient interfaces and class can be optionally implemented or extended by your ActionClass.

  1. Implement Action Interface
  2. Extend ActionSupport class.

First Convenient Approach:

It is to implement the Action interface. This interface is very simple to use. It has five common useful constant values such as SUCCESS, ERROR, NONE, INPUT and LOGIC. Now your action class can use the constant value directly.

The source of this interface is: –

Second Convenient Approach:

It is to use the ActionSupport class, which is a very powerful and convenient class. It provides the default implementation of some of the essential interfaces, and also really easy to extend this class. The basic structure of ActionSupport class is as follows:

Here you can optionally extend this class as follows:


I hope you understand easily these basic concepts of Actions in Struts2 and about ActionsSupport in Struts2. There is a simple and easy way to implement the action in java’s popular framework stuts2.
Here you can know about Facebook launched a new open-source JavaScript engine named ‘Hermes’ to improve the performance of React Native mobile apps on Android – Hermes- A New JavaScript Engine for React Native.
Keep in touch with Habilelabs, which provide best web development support and mobile development services and support for you, contact us if you have something to build.


Author: ChetanChauhan