What is DispatchAction ?

Basically Dispatch action is used when you have multiple submit buttons in a single form.The Dispatch action is a class which extends the Struts DispatchAction , and encpsulates all the action methods (similar to execute method in Action class)in one single class.These actions methods will get executed based on the parameter that you pass in a jsp page.In the Struts-Config.xml you need to define the DispatchAction class and the parameter name that you passing in the URL.Based on the parameter the Controller will invoke the action method.

struts-config.xml
parameter – attribute is used to delegate the request to corresponding method of the BookAction class. So define this value with appropriate name.




 
BookAction.java
It eliminates the execute method entirely.  Because it directly implements the desired methods, those custom methods have same signature as execute method.
The class BookAction extends org.apache.struts.actions.DispatchAction
   import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;

public class BookAction extends DispatchAction
{
public ActionForward addBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Add Book Page");
return mapping.findForward("addBook");
}

public ActionForward editBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Edit Book Page");
return mapping.findForward("editBook");
}

public ActionForward saveBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Save Book Page");
return mapping.findForward("saveBook");
}

public ActionForward deleteBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Delete Book Page");
return mapping.findForward("deleteBook");
}
} 
bookdetails.jsp
This page has four different links for doing different actions say Add/Edit/Save/Delete books. Every individual action has to be uniquely represented by the request parameter actionMethod.
 


Book Details

Add Book
Edit Book
Save Book
Delete Book





Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email