How to use LookupDispatchAction in Struts

This example show you the use of another useful built in struts action LookupDispatchAction class. To use this action you need to extend this LookupDispatchAction class in your action class.

The LookupDispatchAction class is a subclass of DispatchAction and provides a mechanism for modularizing a set of related functions into a single action, thus eliminating the need to create separate, independent actions for each function.

In this example, consider a set of related functions for creating a product, updating a product, deleting a product and reading the information of a product. Instead of creating separate action classes for each type action on product, You can create one Action class by extending LookupDispatchAction, that has four methods: create( ), update( ), read() and delete( ).

At run time, LookupDispatchAction manages routing requests to the appropriate method in its subclass. LookupDispatchAction determines which method to route to base on the value of a request parameter being passed to it from the incoming request.

LookupDispatchAction uses the value of the request parameter to reverse-map to a property in the Struts resource bundle file (i.e. Application.properties). That is, the value of the request parameter is compared against the values of properties in the resource bundle until a match is found. The key for the matching property is then used as a key to another map that maps to a method in your LookupDispatchAction subclass that will be executed.

To use LookupDispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. The subclass must also include a getKeyMethodMap( ) method that maps methods in the class to keys in the Struts resource bundle file. Additionally, you have to set up for the action an action mapping that specifies the name of a request parameter that will be used to select which method will be called for each request.

Below is the example code of LookupDispatchExampleAction class that extends LookupDispatchAction:

package com.java.connect.struts.action;
 
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;
 
public class LookupDispatchExampleAction extends LookupDispatchAction {
 
    @Override
    protected Map getKeyMethodMap() {
        HashMap map = new HashMap();
        map.put("button.create", "create");
        map.put("button.update", "update");
        map.put("button.delete", "delete");
        map.put("button.read", "read");
        return map;
    }
 
    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("create");
    }
 
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("update");
    }
 
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("delete");
    }
 
    public ActionForward read(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("read");
    }
}

You can notice that this class does not provide an implementation for the execute( ) method as other Action classes do. This is because LookupDispatchAction provides to you an implementation of this method that manages delegating to the individual methods. You can also notice the implementation of the getKeyMethodMap( ) method. This method is required by LookupDispatchAction subclasses and is used to map the names of keys in the Struts resource bundle file to methods in the class. The keys’ values in the bundle file are used to match against the value of the incoming request parameter specified by the parameter attribute of the action tag in the Struts configuration file.

In order for your LookupDispatchAction subclass to work, you must create in the Struts configuration file an action mapping that specifies the name of a request parameter that will be used to select the method that will be called for a specific request.

Below is example code of struts configuration for LookupDispatchExampleAction class:

<action path="/lookupDispatchAction" name="productForm"
    type="com.java.connect.struts.action.LookupDispatchExampleAction" scope="request"
    parameter="action">
    <forward name="create" path="/create.jsp" />
    <forward name="update" path="/update.jsp" />
    <forward name="delete" path="/delete.jsp" />
    <forward name="read" path="/read.jsp" />
</action>

The value specified with the parameter attribute of the action tag will be used as the name of a request parameter that will contain the value of a key in the Struts resource bundle shown as below:

button.create=create
button.update=update
button.delete=delete
button.read=read

LookupDispatchAction will use the value of the incoming request parameter to perform a reverse lookup for a key in the resource bundle. The matching key is then mapped to the appropriate method to execute based on the key-to-method mapping specified by the getKeyMethodMap( ) method.

Below is the code of jsp page which used the LookupDispatchAction action

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
    prefix="html"%>
<html>
<head>
</head>
<body>
    <html:form action="lookupDispatchAction" styleId="productForm">
        <table align="center">
            <tr>
                <td>Name : </td>
                <td><html:text property="name" /></td>
            </tr>
            <tr>
                <td>Description : </td>
                <td><html:text property="description" /></td>
            </tr>
            <tr>
                <td>Price : </td>
                <td><html:text property="price" /></td>
            </tr>
            <tr>
                <td>Action : </td>
                <td>
                    <html:select property="action">
                        <html:option value="create">Create</html:option>
                        <html:option value="update">Update</html:option>
                        <html:option value="delete">Delete</html:option>
                        <html:option value="read">Read</html:option>
                    </html:select>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><html:submit value="Got it!" /></td>
            </tr>
        </table>
    </html:form>
</body>
</html>

In this example we have used action select box as the parameter attribute value for LookupDispatchAction mapping.

You can see we are forwarding to the corresponding jsp page based on parameter value and method called in DispatchAction.

Below is the screen show of lookup dispatch action jsp page and success page.

Struts-LookupDispatchAction-tutorial_files_image001

The page will be redirect on the selected action.