Struts Dispatch Action Example
Struts Dispatch Action ([Link]) is one of the Built-in
Actions provided along with the struts framework.
The [Link] 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 [Link] 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 [Link] file (action mapping is shown
later in this page). Here is the code for Action Class.
Developing an Action Class (Dispatch_Action.java)
package [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
* @author Amit Gupta
* @Web [Link]
* @Email struts@[Link]
**/
public class Dispatch_Action extends DispatchAction
{
public ActionForward add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
[Link]("You are in add function.");
return [Link]("add");
}
public ActionForward edit(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
[Link]("You are in edit function.");
return [Link]("edit");
}
public ActionForward search(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
[Link]("You are in search function");
return [Link]("search");
}
public ActionForward save(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
[Link]("You are in save function");
return [Link]("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 ( [Link]):
package [Link];
import [Link];
/**
* @author Amit Gupta
* @Web [Link]
* @Email struts@[Link]
**/
public class DispatchActionForm extends ActionForm
{
private String parameter =" ";
public String getParameter()
{
return parameter;
}
public void setParameter(String parameter)
{
[Link]=parameter;
}
Defining form Bean in [Link] file
Add the following entry in the [Link] file for defining the form bean
<form-bean name="DispatchActionForm"
type="[Link]"/>
Developing the Action Mapping in the [Link]
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="[Link].Dispatch_Action"
parameter="parameter"
input="/pages/[Link]"
name="DispatchActionForm"
scope="request"
validate="false">
<forward name="add" path="/pages/[Link]" />
<forward name="edit" path="/pages/[Link]" />
<forward name="search"
path="/pages/[Link]"/>
<forward name="save" path="/pages/[Link]" />
</action>
Developing jsp page
Code of the jsp ([Link]) to delegate requests to different jsp pages :
<%@ taglib uri="/WEB-INF/[Link]" prefix="bean"%>
<%@ taglib uri="/WEB-INF/[Link]" prefix="html"%>
<html:html locale="true">
<HEAD>
<TITLE>Dispatch Action Example</TITLE>
<BODY>
<H3>Dispatch Action Example</H3>
<p><html:link page="/[Link]?parameter=add">Call Add Section</html:link></p>
<p><html:link page="/[Link]?parameter=edit">Call Edit Section</html:link></p>
<p><html:link page="/[Link]?parameter=search">Call Search
Section</html:link></p>
<p><html:link page="/[Link]?parameter=save">Call Save
Section</html:link></p>
</html:html>
Add the following line in the [Link] to call the form.
<li>
<html:link page="/pages/[Link]">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 [Link] page. Your browser displays the following DispatchAction page.
Selecting Call Add Section displays the following [Link] page
Selecting Call Edit Section displays the following [Link] page
Selecting Call Search Section displays the following [Link] page
Selecting Call Save Section displays the following [Link] page