Monday, August 29, 2011

What is DispatchAction in struts and code Struts Dispatch Action Example and types of action?

 
The different type of actions in Struts are:


    * ForwardAction
    * IncludeAction
    * DispatchAction
    * LookupDispatchAction
    * SwitchAction


What is DispatchAction in struts and code Struts Dispatch Action Example?


The org.apache.struts.actions.DispatchAction class enables a user to collect related functions into a single Action.

DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create seperate actions for each functions. In this example we will see how to group a set of user related actions like add user, update user and delete user into a single action called UserAction.

The class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter. For example if the incoming parameter is "method=add", then the add method will be invoked.


(OR)


The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.




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;

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");


 }


}

No comments:

Post a Comment