How to use DispatchAction in Struts

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

The DispatchAction class 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 DispatchAction, that has four methods: create( ), update( ), read() and delete( ).

At run time, DispatchAction manages routing requests to the appropriate method in its subclass. DispatchAction determines which method to call based on the value of a request parameter that is passed to it from the incoming request.

To use DispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. 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 should be called for each request.

Below is the example of class DispatchExampleAction that extends DispatchAction:

package com.java.connect.struts.action;
 
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;
 
public class DispatchExampleAction extends DispatchAction {
 
    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 the way typical Action classes do. This is because DispatchAction provides to you an implementation of this method that manages delegating to the individual methods. In order for your DispatchAction 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 specific requests.

Below is the code example of struts-config.xml for DispatchAction

<action path="/dispatchAction" name="productForm"
    type="com.java.connect.struts.action.DispatchExampleAction"
    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>

You can see that 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 name of a method to invoke for handling the request.

Below is the example code of jsp file

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
    prefix="html"%>
<html>
<head>
</head>
<body>
    <html:form action="dispatchAction" 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 in DispatchAction 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 dispatch action jsp page.

Struts-DispatchAction-tutorial_files_image001

The page will be redirect on the selected action.

Struts-DispatchAction-tutorial_files_image002