Академический Документы
Профессиональный Документы
Культура Документы
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.
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");
}
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;
}
Add the following entry in the struts-config.xml file for defining the form bean
<form-bean name="DispatchActionForm"
type="roseindia.net.DispatchActionForm"/>
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>
</html:html>
<li>
<html:link page="/pages/DispatchAction.jsp">Struts File Upload</html:link>
<br>
Example demonstrates how DispatchAction Class works.
</li>
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