Struts Dispatch Action

DispatchAction provides a mechanism to group related functions/actions into a single Action class. Therefore each and every action has to be specified in separate methods. This eliminates the creation of multiple independent action classes for each function/action.

DispatchAction class itself has the implementation of execute() method and its being used to managed to delegating the request to one of the methods in the derived Action class. This method invocation can be done based on the request “parameter“.

Let’s see an example of DispatchAction.


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

 <action path="/books"
 <forward name="addBook" path="/jsp/book/addbook.jsp"/>
 <forward name="editBook" path="/jsp/book/updatebook.jsp"/>
 <forward name="saveBook" path="/jsp/book/save.jsp"/>
 <forward name="deleteBook" path="/jsp/book/deletebook.jsp" />

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


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
 <a href="/">Add Book</a>
 <a href="/">Edit Book</a>
 <a href="/">Save Book</a>
 <a href="/">Delete Book</a>

Note: I didn’t enclosed addBook, editBook, saveBook, deleteBook jsp files with this post. You can design and implement in your own way. Here am just explaining the flow of how DispatchAction class works in Struts.

New to Struts, want to learn Struts Basics. Go here

Complete Struts Example, go here


Tags: , , , , , , , , , , ,

One Response to “Struts Dispatch Action”

  1. Rachel Says:

    What’s up, everything is going perfectly here and ofcourse every one is sharing data, that’s genuinely fine, keep up writing.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: