Struts Dispatch Action Example

Struts Dispatch Action (org.apache.struts.actions.DispatchAction) is one of the Built-in Actions provided along with the struts framework.

Struts Dispatch Action Example


Struts Dispatch Action (org.apache.struts.actions.DispatchAction) is one of the Built-in Actions provided along with the struts framework.

The org.apache.struts.actions.DispatchAction class enables a user to collect related functions into a single Action. It eliminates the need of 
creating multiple independent actions for each function. Here in this example you will learn more about Struts Dispatch Action that will help you grasping the concept better.

Let's develop Dispatch_Action class which is a sub class of org.apache.struts.actions.DispatchAction class. This class does not provide an implementation for the execute() method because DispatchAction class itself implements this method. This class  manages to  delegate the request to one of the methods of the derived Action class. An Action Mapping is done to select the particular method (via  Struts-Configuration file). 

Here the Dispatch_Action class contains multiple methods ie.. add() , edit() , search()  , save(). Here all the methods are taking the same input parameters but each method returns a different ActionForward like "add" in case of add() method , "edit" in case of edit() etc. 
Each ActionForward is defined in the struts-config.xml file (action mapping is shown later in this page). Here is the code for Action Class.

Developing an Action Class ( 



  import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

* @author Amit Gupta
* @Web
* @Email [email protected]


public class Dispatch_Action extends DispatchAction


public ActionForward add(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in add function.");
  return mapping.findForward("add");


public ActionForward edit(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception


  System.out.println("You are in edit function.");
    return mapping.findForward("edit");



public ActionForward search(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in search function");
  return mapping.findForward("search");


public ActionForward save(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in save function");
  return mapping.findForward("save");



Developing an ActionForm Class

Our form bean class contains only one property  "parameter" which is playing prime role in this example. Based on the parameter value appropriate function of Action class is executed. Here is the code for  FormBean (


import org.apache.struts.action.ActionForm;


* @author Amit Gupta
* @Web
* @Email [email protected]


public class DispatchActionForm extends ActionForm

  private String parameter =" "; 
  public String getParameter()


  return parameter;


  public void setParameter(String parameter)





Defining form Bean in struts-config.xml file

Add the following entry in the struts-config.xml file for defining the form bean

<form-bean name="DispatchActionForm"

Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests. Note that the value specified with the parameter 
attribute is used to delegate request to the required method  of the Dispath_Action Class.

<forward name="add" path="/pages/DispatchActionAdd.jsp" />
<forward name="edit" path="/pages/DispatchActionEdit.jsp" />
<forward name="search" path="/pages/DispatchActionSearch.jsp"/>
<forward name="save" path="/pages/DispatchActionSave.jsp" />

Developing jsp page

Code of the jsp (DispatchAction.jsp)  to delegate requests to different jsp pages :

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> 
<html:html locale="true">
<TITLE>Dispatch Action Example</TITLE>

<H3>Dispatch Action Example</H3>
<p><html:link page="/">Call Add Section</html:link></p> 
<p><html:link page="/">Call Edit Section</html:link></p> 
<p><html:link page="/">Call Search Section</html:link></p> 
<p><html:link page="/">Call Save Section</html:link></p> 


Add the following line in the index.jsp to call the form.

<html:link page="/pages/DispatchAction.jsp">Struts File Upload</html:link>
Example demonstrates  how DispatchAction Class works.

Building and Testing the Example 

To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the DispatchAction.jsp page. Your browser displays the following DispatchAction page.

Selecting  Call Add Section displays the following DispatchActionAdd.jsp page

Selecting Call Edit Section displays the following   DispatchActionEdit.jsp page

 Selecting Call Search Section displays the following  DispatchActionSearch.jsp page

Selecting Call Save Section  displays the following  DispatchActionSave.jsp  page



Share on Google+Share on Google+

Struts Dispatch Action Example

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Struts Dispatch Action Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 12, 2011
bug in DispatchAction

when i am executing a struts application regarding applying DispatchAction then only 2 errors are coming 1) package import org.apache.struts.actions.*; doesnot exist 2) Symbol DispatchAction doesnot exist except these two all others are resolved. so can u plz guide me regarding this error . Thanking you Manas Ranjan
November 10, 2011

good material.very usefull to understand the concept.
March 22, 2012

Absolutely, I wonder that for even those rieetprnse apps that make extensive use of frameworks whether they get any real benefit out of them.What's the point in making something re-usable if you're never going to re-use it?And if you don't follow it through ALL THE WAY (unit testing, mocking, documentation etc) then chances are it's costing you more to develop, and you're never going to get the payback you should.
Nicholas Migot
October 18, 2012
HTML action

October 6, 2013

This is exactly what I was looking for! thank you, you helped me so much with my poblem.