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
How many hours a day/a week do you work?
Avoid being too specific on this answer. If you give a low figure, the company will view it as inadequate. If you commit
 
Ubuntu 6.06 Beta has been released now
The Ubuntu team is proud to announce the Beta Release of Ubuntu 6.06 LTS - codenamed 'Dapper Drake'.
 
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 MappingDispatchAction Example

                         

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

The org.apache.struts.actions.MappingDispatchAction class is a subclass of org.apache.struts.actions.DispatchAction class. This class enables a user to collect related functions into a single action class. It  needs to create multiple independent actions for each function. Here in this example you will learn more about Struts MappingDispatchAction that will help you to grasp the concept better.

Let's develop a class  MappingDispatch_Action which is a sub class of org.apache.struts.actions.MappingDispatchAction class. This class does not provide an implementation for the execute() method because DispatchAction class itself implements this method.  MappingDispatchAction  class is much like the DispatchAction class except that it uses a unique action corresponding to a new request , to dispatch the methods . At run time, this class  manages to  delegate the request to one of the methods of the derived Action class. Selection of a method depends on the value of  the  parameter  passed from the incoming request. MappingDispatchAction uses this request parameter value and selects a corresponding action  from the different action-mappings defined. This eliminates the need of creating an instance of ActionForm  class. 

MappingDispatch_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). 


Developing an Action Class (MappingDispatch_Action.java) 

  package roseindia.net;

/**
@author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net
**/

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

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

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

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


  
  


}

 

No need to Develop an ActionForm Class

Developing the Action Mapping in the struts-config.xml

Here, we need to create multiple independent actions for each method defined in the action class. Note that the value specified with the
parameter attribute is used to delegate request to the required method  of the MappingDispatch_Action Class.

<action path="/MappingDispatchAction"
type="roseindia.net.MappingDispatch_Action"
parameter="add"
input="/pages/MappingDispatchAction.jsp"
scope="request"
validate="false">
<forward name="add" path="/pages/MappingDispatchActionAdd.jsp" />
</action>

<action path="/MappingDispatchAction"
type="roseindia.net.MappingDispatch_Action"
parameter="edit"
input="/pages/MappingDispatchAction.jsp"
scope="request"
validate="false">
<forward name="edit" path="/pages/MappingDispatchActionEdit.jsp" />
</action>


<action path="/MappingDispatchAction"
type="roseindia.net.MappingDispatch_Action"
parameter="search"
input="/pages/MappingDispatchAction.jsp"
scope="request"
validate="false">
<forward name="search" path="/pages/MappingDispatchActionSearch.jsp"/>
</action>


<action path="/MappingDispatchAction"
type="roseindia.net.MappingDispatch_Action"
parameter="save"
input="/pages/MappingDispatchAction.jsp"
scope="request"
validate="false">
<forward name="save" path="/pages/MappingDispatchActionSave.jsp" />
</action>



 

Developing jsp page

Code of the jsp (MappingDispatchAction.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>Mapping Dispatch Action Example</TITLE>
<BODY>

<H3>Dispatch Action Example</H3>
<p><html:link page="/MappingDispatchAction.do?parameter=add">Call Add Section</html:link></p> 
<p><html:link page="/MappingDispatchAction.do?parameter=edit">Call Edit Section</html:link></p> 
<p><html:link page="/MappingDispatchAction.do?parameter=search">Call Search Section</html:link></p> 
<p><html:link page="/MappingDispatchAction.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/MappingDispatchAction.jsp">Struts File Upload</html:link>
<br>
Example demonstrates  how MappingDispatchAction 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 MappingDispatchAction.jsp page. Your browser displays the following MappingDispatchAction page.

Selecting  Call Add Section displays the following MappingDispatchActionAdd.jsp page

Selecting Call Edit Section displays the following  MappingDispatchActionEdit.jsp page

 Selecting Call Search Section displays the following MappingDispatchActionSearch.jsp page

Selecting Call Save Section  displays the following  MappingDispatchActionSave.jsp  page

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

Current Comments

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

There is a wrong understanding of MappingDispatchAction. In struts-config.xml the action paths must be something like:

<action path="callAdd" parameter="add" .../>

MappingDispatchAction uses the parameter to map the action to the add-method in MappingDispatch_Action. So in the jsp the code is as follows:

<html:link page="/callAdd.do">Call Add Section</html:link>

The Benefit of MappingDispatchAction is, that every action maps to a method only one class, which is like DispatchAction, but the mapping is not done via the parameter attribute, but via the action.

This misunderstanding may come from wrong interpretaion of the parameter attribute. This attribute is a multipurpose parameter for the Action-implementation and does not mean an URL-parameter.

Greetings,
Martin

Posted by Martin Krueger on Friday, 10.19.07 @ 17:57pm | #34398

I could understand much difference between mapping and lookup dispatch action

Posted by debu on Thursday, 06.28.07 @ 18:04pm | #20330

I think this page has been updated lately, and i am not finding any difference of conents between DispatchAction and MappingDispatchAction tutorial. Both are exactly same. Rather the information provided previously was correct and more sensible.

In general tutorial provided by roseindia is quiet useful in all other sense.

Thanks

Posted by angelina on Tuesday, 05.15.07 @ 17:03pm | #15999

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.