Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Struts MappingDispatchAction Example

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

Ads

Tutorials   
Struts 2 Tutorial and Examples Struts 2.5.10.1 General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version 2.3.16.3 released Struts 2 version 2.3.15.3 released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts 2.3.15.2 launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration
Ads

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 [email protected]
**/

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

 

     

Advertisements

Share on Google+Share on Google+

Struts MappingDispatchAction Example

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Struts MappingDispatchAction Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Gustavo Amaro
June 4, 2014
Error

It's not working... only calls the last method (save())... :(

Ads

 

Ads