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.

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.

 <action path="/books"
 type="com.books.BookAction"
 parameter="actionMethod"
 validate="false"
 scope="request">
 <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" />
 </action>
 

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.

 <html>
 <head>
 Book Details
 <body>
 <a href="/books.do?actionMethod=addBook">Add Book</a>
 <a href="/books.do?actionMethod=editBook">Edit Book</a>
 <a href="/books.do?actionMethod=saveBook">Save Book</a>
 <a href="/books.do?actionMethod=deleteBook">Delete Book</a>
 </body>
 </head>
 </html>

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 http://manikandanmv.wordpress.com/2010/11/18/struts-basics/

Complete Struts Example, go here  http://manikandanmv.wordpress.com/2010/12/07/struts-example/

Advertisement

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.