Struts EventDispatchAction

An Action that dispatches to to one of the public methods that are named in the parameter attribute of the corresponding ActionMapping and matches a submission parameter. This is useful for developers who prefer to use many submit buttons, images, or submit links on a single form and whose related actions exist in a single Action class.

A convenient way to select a dispatch method is by linking it with a button. This can be problematic in a localized application, since the label of the button may change according to the user’s locale. For one user, the button may read delete; for another, it may read aufteilen.

The method(s) must have the same signature (other than method name) of the standard Action.execute method.

To configure the use of this action in your struts-config.xml file, create an entry like this:

<action  path="/EventDispatchAction"
            type="com.kruders.action.CalculatorAction" name="CalculatorForm"
            parameter="add,subtract,multiply,divide,default=add">
    <forward name="success" path="/calculator.jsp"/>
</action>

In a JSP, the buttons could be created like this:

where parameter contains three possible methods and one default method if nothing matches (such as the user pressing the enter key). The default key is purely optional. If this is not specified and no parameters match the list of method keys, null is returned which means the unspecified method will be invoked.

<html:form action="LookupDispatchAction">
    <html:submit property="method">
        <bean:message key="label.add" />
    </html:submit>
</html:form>

Difference between the DispatchAction and EventDispatch – we define 1 mapping per method for DispatchAction, while we map all the methods in a single mapping for EventDispatchAction

1. Action Form

Create a Action Form class and name it CalculatorForm.java that contains the following code.

package com.kruders.form;
 
import org.apache.struts.action.ActionForm;
 
public class CalculatorForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private Integer numberA;
    private Integer numberB;
    public Integer getNumberA() {
        return numberA;
    }
    public void setNumberA(Integer numberA) {
        this.numberA = numberA;
    }
    public Integer getNumberB() {
        return numberB;
    }
}

2. Action Class (Controller)

Let us create an Action class that will handle the request.

package com.kruders.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;
 
import com.kruders.form.CalculatorForm;
 
public class CalculatorAction extends LookupDispatchAction{
    public ActionForward add(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
        CalculatorForm calculatorForm = (CalculatorForm) form;
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = calculatorForm.getNumberA() + calculatorForm.getNumberB();
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward subtract(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA - numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward multiply(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA * numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward divide(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA / numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
}

3. Action Handler

The next step is to create an action mapping for this action handler. The request parameter name is specified using the parameter attribute. Here the request parameter name is method.

In struts-config.xml write the following code

<action path="/LookupDispatchAction" parameter="add,subtract,multiply,divide,default=add" name="CalculatorForm" type="com.kruders.action.CalculatorAction">
    <forward name="success" path="/calculator.jsp" />
</action>

4. Create JSP

Now create calculator.jsp and write the following code.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>EventDispatchAction Page</title>
</head>
<body>
<html:form action="EventDispatchAction">
    Number A <html:text name="CalculatorForm" property="numberA" /><br>
    Number B <html:text name="CalculatorForm" property="numberB" /><br>
    <html:submit property="method">
        <bean:message key="label.add" />
    </html:submit>  
    <html:submit property="method">
        <bean:message key="label.subtract" />
    </html:submit>  
    <html:submit property="method">
        <bean:message key="label.multiply" />
    </html:submit>  
    <html:submit property="method">
        <bean:message key="label.divide" />
    </html:submit>
</html:form>
<% String result = (String)session.getAttribute("result");   
   if(result!=null) out.println("<br>Result : " + result);%>   
</body>
</html>

5. Struts Config

struts-config.xml file contains the following code.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
    <form-beans>
        <form-bean name="CalculatorForm" type="com.kruders.form.CalculatorForm" />
    </form-beans>
     
    <global-forwards>
        <forward name="calculator" path="/calculator.do"/>
    </global-forwards>
     
    <action-mappings>
     
        <action
            path="/calculator"
            type="org.apache.struts.actions.ForwardAction"
            parameter="/calculator.jsp"/>
         
        <action
            path="/EventDispatchAction"
            type="com.kruders.action.CalculatorAction" name="CalculatorForm"
            parameter="add,subtract,multiply,divide,default=add">
            <forward name="success" path="/calculator.jsp"/>
        </action>
 
    </action-mappings>
    <message-resources parameter="ApplicationResource"/>
</struts-config>

6. Properties File

Create ApplicationResources.properties write the following text.

label.add = add
label.subtract = subtract
label.multiply = multiply
label.divide = divide

7. Run Project

Now when you run the project, following screen will be displayed as in Figure 12.1

Figure 12.1 Figure 12.1


Now enter numberA and numberB and click on Add button, following screen will be displayed as in Figure 12.2

Figure 12.2 Figure 12.2

The folder structure of the example is shown below in Figure 12.3

Figure 12.3 Figure 12.3

You can download the source code of this example here.

One Response to Struts EventDispatchAction