DispatchAction in struts example

As per previous tutorials, we need separate controller support classes or action class for every operations we perform in our struts enabled web application. So, it will be really hazy to maintain so much number of custom action classes. How we will solve the problem? Struts 1 has provide an action class that will make us possible to group a number of related operations in a single class and i.e. DispatchAction class. Thank GOD.

In the struts 1.3 release the DispatchAction has been move to a new jar. You can download that jar from download link : struts-extras-1.3.10

Here I will illustrate an example witch uses DispatchAction to group a number of operations in a single class. Let us first understand our example requirement :

My big bro’s marriage is near but I have not list our my guest names for sending invitation. I need an application that will provide following facilities :

  • I Should be able to add a name to the list.
  • I Should be able to delete a name from list.
  • I should be able to edit a name from the list.
  • And finally the name list should be shown in the jsp page.

So there are four operations that are of same type, so decided to use DispatchAction in my aplication.

The following will be flow of developing the example of DispatchAction :

  1. Firstly, we have to create a pojo class “Person.java” that will store firstname and lastname of a person to be invited.
  2. Then, we will create form bean class “InvitationForm.java” that will contain a map (HashMap). This map will hold all the persons that are in invitation list and an instance of person class, that will hold the person on which an operation is to be perform the operations (add, delete and edit).
  3. After that, we will implement our controller support class (InvitationAction.java) that will extend DispatchAction class provided in struts 1.
  4. Then, we will create our jsp page (index.jsp) that will show all the users in the map of class InvitationForm and form to add or edit a particular person.
  5. And then last but not the lease, we will configure out struts-config.xml for implementing our application finally.


public class Person {
    private String firstName = null;
    private String lastName = null;

    // Setter, getter and constructor are omitted for making the file short

    public String toString() {
        return firstName + " " + lastName;

Person class contains two fields “firstName” and “lastName”, that will store first name and last name of a person to be invited. This class is not related to struts mvc framework but I have implemented it to make the example more life implementation.


It may be little bit confusing for you to under stand the code of InvitationForm.java, but it will help you to solve real life industrial problems in you life. See the code bellow :

public class InvitationForm extends ActionForm {

    private static final long serialVersionUID = -4698158775973183265L;

    private Map<Long, Person> personMap = new HashMap<Long, Person>();
    private Person currentPerson = new Person();
    private long personId = new Date().getTime();

    public Map<Long, Person> getPersonMap() {
        return personMap;

    public void setPersonMap(Map<Long, Person> personList) {
        this.personMap = personList;

    public String getFirstName() {
        return currentPerson.getFirstName();

    public void setFirstName(String firstName) {

    public String getLastName() {
        return currentPerson.getLastName();

    public void setLastName(String lastName) {

    public long getKey() {
        return personId;

    public void setKey(long personId) {
        this.personId = personId;

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.personId = new Date().getTime();
        super.reset(mapping, request);

As you can see, there is a HashMap to hold the list of persons in the form bean so that we can print them all in jsp as well as we have taken an instance of person and long type variable that will hold the id of the person instance. To make the id unique, I have taken “new Date().getTime()” as its initial value that will provide a unique id for every person that is added to the list or of the person instance in the form bean. The maintain of HashMap will be done according to the operation in action class.


The complicated and central part of this tutorial is InvitationAction.java. The code is shown bellow :

package com.raistudies.actions;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.raistudies.domain.Person;
import com.raistudies.forms.InvitationForm;

public class InvitationAction extends DispatchAction{

    public ActionForward add(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response){
        InvitationForm invForm = (InvitationForm)form;
        Person newPerson = new Person();
        invForm.getPersonMap().put(invForm.getKey(), newPerson);
        invForm.reset(mapping, request);
        return mapping.findForward("success");

    public ActionForward delete(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response){
        InvitationForm invForm = (InvitationForm)form;
        return mapping.findForward("success");

    public ActionForward edit(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response){
        InvitationForm invForm = (InvitationForm)form;
        Person person = invForm.getPersonMap().get(invForm.getKey());
        return mapping.findForward("success");

You may be thinking that I forget to implement “execute” method in our action class. But not like that out action class is extending DispatchAction which is a subclass of Action class. You can see, there are three methods in our action class with the same signature as “execute” does but the name of the methods are different. If our action extends DispatchAction class, we can define our won method name with same signature as “execute” does. We can specify the name of the method to be executed from the submit input of html form. So, in this way we can define similar operation in a single class execute them as per our need.


We will learn some new struts UI tags in this jsp that are used to print person names in HashMap to jsp. Here is the code of index.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <title>Invitation List</title>
        Name of the Persons to be invited : <br>
        <logic:present name="invitationForm">
            <logic:iterate id="element" name="invitationForm" property="personMap">
                <html:form action="/invitation"><html:hidden property="key" name="element"/><bean:write name="element" property="value"/>
                    <html:submit value="edit" property="action"/>
                    <html:submit value="delete" property="action"/>
        Add/Edit a person in the list :
        <html:form action="/invitation">
        <html:hidden property="key"/>
        First Name : <html:text property="firstName"/>
        Last Name : <html:text property="lastName"/>
        <html:submit property="action" value="add"/>

The index.jsp is been coded to work as following screen shot :

After First Name Add

After First Name Add

As you can see in screen shot, when we will add a person in list it will be shown in the list and two option for this person will be shown “edit” and “delete”. <html:submit value=”edit” property=”action”/> tells that it will call edit method in our InvitationAction and same for “delete” and “add” button.


    <!-- ========== Form Bean Definitions ================================== -->
        <form-bean name="invitationForm" type="com.raistudies.forms.InvitationForm"/>
    <!-- ========== Action Mapping Definitions ============================= -->
        <action name="invitationForm" path="/invitation" type="com.raistudies.actions.InvitationAction"
        scope="session" input="/index.jsp" parameter="action">
            <forward name="success" path="/index.jsp" redirect="true"/>

The above code shows the configuration of our action InvitationAction that extends DispatchAction. One new thing here is the attributeparameter=”action” which indicated the name of the parameter used in <html:submit/> tag. As we have mention <html:submit value=”edit” property=”action”/> in our jsp that means the value of the property “action” is “edit” and we have also mentioned in the action configuration that parameter=”action”, so struts will execute the method named with the value of the property name “action” i.e. “edit“. The summary is that on the press of edit button edit method of the action will be executed.

When we will deploy our application in tomcat, we will get the following ui :

Initial Form

Initial Form

Then click on add button, you will get first image shown in this tutorial. Then click on edit button, you will see the values will come in input boxes automatically :

Edit Name Option

Edit Name Option

Huraaaa!!!! Now I can easily create my invitation list.

You can download the source code from bellow links:

Source : Download

War : Download

Related Posts:

Leave a comment ?


  1. How to use the validate method in this program?

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>