Struts Dispatch Action Example

Struts Dispatch Action (org.apache.struts.actions.DispatchAction) is one of the Built-in Actions provided along with the struts framework.

Struts Dispatch Action Example

Struts Dispatch Action Example

     

Struts Dispatch Action (org.apache.struts.actions.DispatchAction) is one of the Built-in Actions provided along with the struts framework.

The org.apache.struts.actions.DispatchAction class enables a user to collect related functions into a single Action. It eliminates the need of 
creating multiple independent actions for each function. Here in this example you will learn more about Struts Dispatch Action that will help you grasping the concept better.

Let's develop Dispatch_Action class which is a sub class of org.apache.struts.actions.DispatchAction class. This class does not provide an implementation for the execute() method because DispatchAction class itself implements this method. This class  manages to  delegate the request to one of the methods of the derived Action class. An Action Mapping is done to select the particular method (via  Struts-Configuration file). 

Here the Dispatch_Action class contains multiple methods ie.. add() , edit() , search()  , save(). Here all the methods are taking the same input parameters but each method returns a different ActionForward like "add" in case of add() method , "edit" in case of edit() etc. 
Each ActionForward is defined in the struts-config.xml file (action mapping is shown later in this page). Here is the code for Action Class.

Developing an Action Class (Dispatch_Action.java) 

package roseindia.net;

 

import java.io.*;
  import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email [email protected]

**/  

public class Dispatch_Action extends DispatchAction
 

{
  

public ActionForward add(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in add function.");
  return mapping.findForward("add");
  }

  

public ActionForward edit(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception

 {


  System.out.println("You are in edit function.");
    return mapping.findForward("edit");
  

 }

 

public ActionForward search(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in search function");
  return mapping.findForward("search");
  

 }
  
  

public ActionForward save(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception{
  System.out.println("You are in save function");
  return mapping.findForward("save");
 

 }
 

}

Developing an ActionForm Class

Our form bean class contains only one property  "parameter" which is playing prime role in this example. Based on the parameter value appropriate function of Action class is executed. Here is the code for  FormBean ( DispatchActionForm.java):

package roseindia.net;
 

import org.apache.struts.action.ActionForm;
 

/**
 

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email [email protected]
 

**/
 

public class DispatchActionForm extends ActionForm
{
  

  private String parameter =" "; 
  public String getParameter()
  

   {
  

  return parameter;
  

   }

0


  public void setParameter(String parameter)
  

   {
  

  this.parameter=parameter;
  

1

   }
  

}

Defining form Bean in struts-config.xml file

Add the following entry in the struts-config.xml file for defining the form bean

2
<form-bean name="DispatchActionForm"
   type="roseindia.net.DispatchActionForm"/>

Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests. Note that the value specified with the parameter 
attribute is used to delegate request to the required method  of the Dispath_Action Class.

<action
path="/DispatchAction"
type="roseindia.net.Dispatch_Action"
parameter="parameter"
input="/pages/DispatchAction.jsp"
name="DispatchActionForm"
scope="request"
validate="false">
<forward name="add" path="/pages/DispatchActionAdd.jsp" />
<forward name="edit" path="/pages/DispatchActionEdit.jsp" />
<forward name="search" path="/pages/DispatchActionSearch.jsp"/>
<forward name="save" path="/pages/DispatchActionSave.jsp" />
</action>

Developing jsp page

Code of the jsp (DispatchAction.jsp)  to delegate requests to different jsp pages :

3
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> 
<html:html locale="true">
<HEAD>
<TITLE>Dispatch Action Example</TITLE>
<BODY>

<H3>Dispatch Action Example</H3>
<p><html:link page="/DispatchAction.do?parameter=add">Call Add Section</html:link></p> 
<p><html:link page="/DispatchAction.do?parameter=edit">Call Edit Section</html:link></p> 
<p><html:link page="/DispatchAction.do?parameter=search">Call Search Section</html:link></p> 
<p><html:link page="/DispatchAction.do?parameter=save">Call Save Section</html:link></p> 

</html:html>

Add the following line in the index.jsp to call the form.

<li>
<html:link page="/pages/DispatchAction.jsp">Struts File Upload</html:link>
<br>
Example demonstrates  how DispatchAction Class works.
</li>

Building and Testing the Example 

4

To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the DispatchAction.jsp page. Your browser displays the following DispatchAction page.

Selecting  Call Add Section displays the following DispatchActionAdd.jsp page

Selecting Call Edit Section displays the following   DispatchActionEdit.jsp page

5

 Selecting Call Search Section displays the following  DispatchActionSearch.jsp page

Selecting Call Save Section  displays the following  DispatchActionSave.jsp  page

 

6

     

Tutorials

  1. Validations using Struts 2 Annotations
  2. Struts 1.1 Tutorials
  3. CRUD application in hibernate annotation
  4. Understanding Spring Struts Hibernate DAO Layer
  5. DAO Layer explained
  6. Developing Forgot Password Form
  7. Welcome to the Apache Struts Tutorial
  8. Developing Simple Struts Tiles Application
  9. Understanding Struts Controller
  10. Struts Hibernate Integration
  11. Developing Struts PlugIn
  12. Developing Struts Hibernate and Spring Based Login/Registration Application
  13. Struts File Upload and Save
  14. Struts 2 Features
  15. Struts 2 - History of Struts 2
  16. Struts 2 Architecture - Detail information on Struts 2 Architecture
  17. Download and Installing Struts 2
  18. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  19. Developing JSP, Java and Configuration for Hello World Application
  20. Struts Configuration file - struts.xml
  21. Introduction to Struts 2 Tags
  22. Struts Logic Tags: An Introduction
  23. Logic Empty Tag (...)
  24. Logic Equal Tag (...)
  25. Logic greaterEqual Tag (... )
  26. Logic LessEqual Tag (...)
  27. Logic Match Tag (...)
  28. Logic Present Tag (...)
  29. Struts2 Actions
  30. Static Parameter
  31. Accessing Session Object
  32. Access Request and Response
  33. Control Tags-If / Else If / Else
  34. Append Tag (Control Tags) Example
  35. Generator Tag (Control Tags) Example
  36. Generator Tag (Control Tags) Using Count Attributes
  37. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  38. Iterator Tag (Control Tags) Example
  39. Merge Tag (Control Tags) Example
  40. Subset Tag (Control Tags) Example