LookupDispatchAction Example

DispatchAction is perfect for using in scenarios where some common data is modified with different actions as we seen in the previous tutorial. But, there is only one problem with this approach of merging common actions in one DispatchAction i.e. The web part of the application will show which method is to be called for what action. That may be security leak from the prospective of a high risk applications like banking application. To solve the problem Struts also provide a new Action called LookupDispatchAction. If we use to extend this action then we will specify the name of the method in a properties file which will be a secure.

This tutorial is assuming that you have read previous tutorial of DispatchAction Example. If you have not read it, it is recommended to read.

Tools Used :

  • Struts 1.3
  • Eclipse Indigo 3.7
  • Tomcat 6
  • Jdk 1.6

In this tutorial, we will consider the same example of creating a list of person as we do in DispatchAction example. The only difference will be that, here we will use LookupDispatchAction in place of DispatchAction.

  • We will perform following changes to the application to make it work with LoopupDispatchAction:
  • Our action class (InvitationAction.java) will extend LoopupDispatchAction instead of DispatchAction.
  • Add a application properties file that will contain the detail of the method to be executed when a particular value will come from the request parameter which is defined in attributeparameter in action tag.
  • Some basic changes to jsp for making the ui more perfect.

We do not required any change to domain class and form.

Our Action class (InvitationAction.java)

Here is our action class that extends LoopupDispatchAction and overrides the method getKeyMethodMap:


package com.raistudies.actions;

import java.util.HashMap;
import java.util.Map;

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.LookupDispatchAction;

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

public class InvitationAction extends LookupDispatchAction{

    public ActionForward add(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response){
        InvitationForm invForm = (InvitationForm)form;
        Person newPerson = new Person();
        newPerson.setFirstName(invForm.getFirstName());
        newPerson.setLastName(invForm.getLastName());
        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;
        invForm.getPersonMap().remove(invForm.getKey());
        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());
        invForm.setFirstName(person.getFirstName());
        invForm.setLastName(person.getLastName());
        return mapping.findForward("success");
    }

    @Override
    protected Map getKeyMethodMap() {
        Map keyMap = new HashMap();
        keyMap.put("addKey", "add");
        keyMap.put("editKey", "edit");
        keyMap.put("deleteKey", "delete");
        return keyMap;
    }
}

getKeyMethodMap returns a Map that assigns every action method present in the class to a key value.

keyMap.put(“addKey”, “add”);

The above line assigned the add method to the key addKey. This key will be used by the LookupDispatchAction to determine the method to be executed.

Application properties file added to the application


addKey=Add Person
editKey=Edit Person
deleteKey=Delete Person

The apps.properties file defines the method key and the value of the parameter assigned to it.

Struts Configuration file


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <!-- ========== Form Bean Definitions ================================== -->
    <form-beans>
        <form-bean name="invitationForm" type="com.raistudies.forms.InvitationForm" />
    </form-beans>

    <!-- ========== Action Mapping Definitions ============================= -->
    <action-mappings>

        <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" />
        </action>

    </action-mappings>
    <message-resources parameter="apps"/>

</struts-config>

As you can see, we have mentioned parameter=”action” in action tag. So the LookupDispatchAction will first find the value of the “action” request parameter. Consider it is “Add Person”, now it will look into the apps.properties to file out the key that has been assigned to the value “Add Person” i.e. “addKey”. Then LookupDispatchAction will call the method getKeyMethodMap to get the map instance and and fine the method that has been assigned to the key “addKey” i.e. “add”. So it will execute the method “add“.

Run the application on Tomcat 6 and hit the url in browser, you will get following screen:

LookupDispatchAction Example Invitation List

LookupDispatchAction Example Invitation List

Specify first name and last name of persons and click on “Add Person” button. You will see that the persons will be added to the list:

LookupDispatchAction Example Invitation List with Added Person

LookupDispatchAction Example Invitation List with Added Person

You can also edit a name by clicking on the “Edit Person” button:

LookupDispatchAction Example Invitation List in Edit Person Mode

LookupDispatchAction Example Invitation List in Edit Person Mode

Again click on the “Add Person” button to complete the editing.

LookupDispatchAction Example Invitation List in Edit Complete of Person

LookupDispatchAction Example Invitation List in Edit Complete of Person

You can download source code of the tutorial example from following links:

Source: Download

Source + lib: Download

Related Posts:

Leave a comment ?

3 Comments.

  1. Really superb website for struts beginners.
    Looking forward for more real life industrial examples.

  2. Hi,

    Very nice example for struts beginners.. In the above example we are adding the persons.. Here i want to add these persons into my database.. How to do this..? Please let me know..

    Thanks in Advance
    Lalitha

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>