### DispatchAction Functionality in Struts 2

Posted by on Sep 12, 2013 in Struts 2 Tutorial, Struts-2 | 12 comments

In Struts 1 DispatchAction helps us in grouping a set of related functions into a single action. In Struts 2 all the Actions by default provide this functionality. To use this functionality we need to create different methods with the similar signature of the execute() method. In our example the CalculatorAction class has all the methods related to a Calculator like add(), multiply(), division() and subtract() which are used for performing mathematical operation on two numbers and it displays the result in a single jsp based on the action button clicked in jsp.

** UPDATE: Struts 2 Complete tutorial now available here.

#### Action Class

```package com.simplecode.action;

import com.opensymphony.xwork2.Action;

public class CalculatorAction implements Action
{
private float number1;
private float number2;
private float result;
private String methodName;

public String execute()
{
number1 = 0;
number2 = 0;
result = 0;
setMethodName("execute Method");
return SUCCESS;
}

{
result=number1+number2;
}

public String subtract()
{
result = number1 - number2;
setMethodName("subtract Method");
return "subtract";
}

public String multiply()
{
result = number1 * number2;
setMethodName("multiply Method");
return "multiply";
}

public String divide()
{
if(number2!=0)
result = number1/number2;
else if(number1!=0)
result = number2/number1;
else
result=0;

setMethodName("divide Method");
return "divide";
}

public float getNumber1() {
return number1;
}

public void setNumber1(float number1) {
this.number1 = number1;
}

public float getNumber2() {
return number2;
}

public void setNumber2(float number2) {
this.number2 = number2;
}

public float getResult() {
return result;
}

public void setResult(float result) {
this.result = result;
}

public String getMethodName() {
return methodName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
}
}
```

Recommended Article

#### Struts.xml

Here in order to do a DispatchAction functionality we need to create separate action mapping for each method in the action class, which is done in the struts.xml as shown below.

```<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="default" extends="struts-default">

<action name="Number" class="com.simplecode.action.CalculatorAction">
<result name="success">/curd.jsp</result>
</action>
class="com.simplecode.action.CalculatorAction">
</action>
<action name="multiplytNumber" method="multiply"
class="com.simplecode.action.CalculatorAction">
<result name="multiply">/curd.jsp</result>
</action>
<action name="subtractNumber" method="subtract"
class="com.simplecode.action.CalculatorAction">
<result name="subtract">/curd.jsp</result>
</action>
<action name="divideNumber" method="divide"
class="com.simplecode.action.CalculatorAction">
<result name="divide">/curd.jsp</result>
</action>

</package>
</struts>
```

Here note that we use the same action class in all the action mappings but only the method name differs. So now When the request URL is “Number” the execute() method in the CalculatorAction class will be executed. When the request URL is “addNumber” the add() method in the CalculatorAction class will be invoked, this is specified using the method attribute of the action tag in struts xml. Similarly for subtract, multiply and divide request the subtract() ,multiply() and divide() methods will be invoked respectively.

This way configuring a separate action mapping for each method of same action class can be avoided by using a feature called Dynamic Method Invocation. We will learn about this in our upcoming tutorial.

#### Jsp

In the curd.jsp page we create five different buttons to invoke the different methods in the CalculatorAction class.

```<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<title>Dispatch Action</title>
<body>
<s:form action="Number">
<table>
<tr><td><s:textfield name="number1" label="Number 1 " id="number1"/></td></tr>
<tr><td><s:textfield name="number2" label="Number 2 " id="number2"/></td></tr>
<tr><td><s:textfield name="result" label="Result " readonly="true" /></td></tr>
<tr>
<td><s:textfield name="methodName" label="Method involked " readonly="true" /></td>
</tr>
<tr>
<td><s:submit action="subtractNumber" value="Subtract" align="left"/></td>
<td><s:submit action="divideNumber" value="Divide" align="left"/></td>
<td><s:submit action="multiplytNumber" value="Multiply" align="left"/></td>
<td><s:submit align="left"/></td>
</tr>
</table>
</s:form>
</body>
</html>
```

On running the example the following page will be displayed in the browser. Now when the user click a button the appropriate method in the CalculatorAction class gets invoked. For example When the user clicks the add button the add() method in the CalculatorAction class gets executed and the following page is displayed. Note: Dispatch action functionality is used when you have multiple submit buttons in a single form.