Tuesday, June 30, 2015

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.

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.

 
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()
       
{
                result
=number1+number2;
                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()
       
{
               
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;
       
}
}

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.
 
 
<!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
 
        </action>
 
        <action name="addNumber" method="add"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="add">/curd.jsp
 
        </action>
 
        <action name="multiplytNumber" method="multiply"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="multiply">/curd.jsp
 
        </action>
 
        <action name="subtractNumber" method="subtract"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="subtract">/curd.jsp
 
        </action>
 
                <action name="divideNumber" method="divide"
 
                class="com.simplecode.action.CalculatorAction">
 
                <result name="divide">/curd.jsp
 
        </action>
 
 
 
</package>
 
</struts>



Here not
e 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"%>





Dispatch Action










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
 


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

 Good One.