Pages Navigation Menu

Coding is much easier than you think

DispatchAction Functionality in Struts 2

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.
Dispatchaction functionality in struts 2
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;

	public String add() 
		setMethodName("add Method");
		return "add";

	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() 
			result = number1/number2;
		else if(number1!=0)
			result = number2/number1;
		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



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" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

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

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


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.



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"%>
<title>Dispatch Action</title>
<s:form action="Number">
	<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>
        <td><s:textfield name="methodName" label="Method involked " readonly="true" /></td>
	    <td><s:submit action="addNumber" value="Add" align="left"/></td>
       	    <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>	

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.
DispatchAction Method

Note: Dispatch action functionality is used when you have multiple submit buttons in a single form.


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.