Services | Updates | Contact
Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Open Source RFID
RFID RFID systems can be used just about anywhere, from clothing tags to missiles to pet tags to food -- anywhere that
 
Download Quartz Job Scheduler
In this section we will download Quartz Job Scheduler from its distribution web site and then create development environ
 
More Tutorials...


    Loan Information     Struts     Open Source

Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

 
 
Struts

 
Comments
 
 

 

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 struts@roseindia.net

**/  

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 struts@roseindia.net
 

**/
 

public class DispatchActionForm extends ActionForm
{
  

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

   {
    

    return parameter;
  

       }


  public void setParameter(String parameter)
  

   {
    

    this.parameter=parameter;
  

   }
  

}



Defining form Bean in struts-config.xml file

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

<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 :

<%@ 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 

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

 Selecting Call Search Section displays the following  DispatchActionSearch.jsp page

Selecting Call Save Section  displays the following  DispatchActionSave.jsp  page

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

20 comments so far (post your own) View All Comments Latest 10 Comments:

its very helpful ..

Posted by sandeep on Thursday, 05.8.08 @ 09:10am | #58868

hi...i think action is missing in ur dispatchaction.jsp file..go through and send me a correct solution..while executing the programm output came but in linking fashoin just call add section came ..below line not camee.can u find out error and send solution as early as possible..thanks in advance...

Posted by vamsi on Saturday, 04.19.08 @ 10:56am | #57123

I have a small dought can we write multipul methods in Action class like dispatch Action.
In Action class some time we will write two methods execute(),iscancel().

Posted by Mahesh on Monday, 01.7.08 @ 12:35pm | #44693

As far as I know we need to add code for dispatchMethod to dipatch to perticular method.
Do we need this method or not?
Can somebody please clear this confusion?

Posted by Bhushan Chirmade on Friday, 12.28.07 @ 11:49am | #44025


It educed me a lot when concerned with STRUTS.
I whole heartedly appreciate you work for publishing such a valuable information on web.

Posted by vamsi krishna.kunasani on Monday, 12.3.07 @ 20:25pm | #41190

hi
plese someone tell me how to use js files in struts when using dispatchaction

Posted by Raghavendra on Wednesday, 11.21.07 @ 14:37pm | #38152

Hii
I tried this example and when i click on a link i have this error:

HTTP Status 500 - No action instance for path /DispatchAction could be created

message No action instance for path /DispatchAction could be created

description The server encountered an internal error (No action instance for path /DispatchAction could be created) that prevented it from fulfilling this request.

Does anyone know the soluction??
Please it's URGENT!!!!

Posted by layal on Tuesday, 11.20.07 @ 21:23pm | #38079

Examples on how to use DispatchAction

Posted by Srinivas.S on Monday, 11.19.07 @ 12:29pm | #37794

It is good to see the dispatcher action class code and it's site very simple to learn and easy to understand.I really like it this site.

Posted by Harishkumar on Friday, 09.21.07 @ 14:47pm | #28532

good site to get basic idea

Posted by nagesh on Wednesday, 09.12.07 @ 12:15pm | #26825

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.