Understanding Struts Action Class

In this lesson I will show you how to use Struts Action Class and forward a jsp file through it.

Understanding Struts Action Class

Understanding Struts Action Class


In this lesson I will show you how to use Struts Action Class and forward a jsp file through it. 

What is Action Class?

An Action class in the struts application extends Struts 'org.apache.struts.action.Action" Class. Action class acts as wrapper around the business logic and provides an inteface to the application's Model layer. It acts as glue between the View and Model layer. It also transfers the data from the view layer to the specific business process layer and finally returns the procssed data from business layer to the view layer.

An Action works as an adapter between the contents of an incoming HTTP request and the business logic that corresponds to it. Then the struts controller (ActionServlet) slects an appropriate Action and creates an instance if necessary, and finally calls execute method.

To use the Action, we need to  Subclass and overwrite the execute() method. In the Action Class don't add the business process logic, instead move the database and business process logic to the process or dao layer.

The ActionServlet (commad) passes the parameterized class to Action Form using the execute() method. The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object.

Developing our Action Class?

Our Action class (TestAction.java) is simple class that only forwards the TestAction.jsp. Our Action class returns the ActionForward  called "testAction", which is defined in the struts-config.xml file (action mapping is show later in this page). Here is code of our Action Class:


package roseindia.net;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestAction extends Action
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse responsethrows Exception{
  return mapping.findForward("testAction");

Understanding Action Class
Here is the signature of the Action Class.

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             javax.servlet.http.HttpServletRequest request,
                             javax.servlet.http.HttpServletResponse response)
                      throws java.lang.Exception

Action Class process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic. Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.


mapping - The ActionMapping used to select this instance
form - The optional ActionForm bean for this request (if any)
request - The HTTP request we are processing
response - The HTTP response we are creating
Action class throws java.lang.Exception - if the application business logic throws an exception

Adding the Action Mapping in the struts-config.xml
To test the application we will add a link in the index.jsp 
<html:link page="/TestAction.do">Test the Action</html:link>

Following code under the <action-mappings> tag is used to for mapping the TestAction class.

      <forward name="testAction" path="/pages/TestAction.jsp"/>

To test the new application click on Test the Action link on the index page. The content of TestAction.jsp should be displayed on the user browser.

In this lesson you learned how to create Action Class and add the mappings in the struts-config.xml. Our Action Class returns the ActionForward  mapping of the TestAction.jsp.



  1. Validations using Struts 2 Annotations
  2. Struts 1.1 Tutorials
  3. CRUD application in hibernate annotation
  4. Understanding Spring Struts Hibernate DAO Layer
  5. DAO Layer explained
  6. Developing Forgot Password Form
  7. Welcome to the Apache Struts Tutorial
  8. Developing Simple Struts Tiles Application
  9. Understanding Struts Controller
  10. Struts Hibernate Integration
  11. Developing Struts PlugIn
  12. Developing Struts Hibernate and Spring Based Login/Registration Application
  13. Struts File Upload and Save
  14. Struts 2 Features
  15. Struts 2 - History of Struts 2
  16. Struts 2 Architecture - Detail information on Struts 2 Architecture
  17. Download and Installing Struts 2
  18. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  19. Developing JSP, Java and Configuration for Hello World Application
  20. Struts Configuration file - struts.xml
  21. Introduction to Struts 2 Tags
  22. Struts Logic Tags: An Introduction
  23. Logic Empty Tag (...)
  24. Logic Equal Tag (...)
  25. Logic greaterEqual Tag (... )
  26. Logic LessEqual Tag (...)
  27. Logic Match Tag (...)
  28. Logic Present Tag (...)
  29. Struts2 Actions
  30. Static Parameter
  31. Accessing Session Object
  32. Access Request and Response
  33. Control Tags-If / Else If / Else
  34. Append Tag (Control Tags) Example
  35. Generator Tag (Control Tags) Example
  36. Generator Tag (Control Tags) Using Count Attributes
  37. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  38. Iterator Tag (Control Tags) Example
  39. Merge Tag (Control Tags) Example
  40. Subset Tag (Control Tags) Example