DispatchAction in struts 1.3. How it works?

The org.apache.struts.actions.DispatchAction class provides a mechanism for modularizing a set of related functions into a single action. It eliminates the need to create separate independent actions for each function. In Simple Action class will have only one method called execute. DispatchAction class enables a user to write related multiple methods(Functions) into a single Action class. It eliminates the need of creating multiple actions for each separate functionality.

For example Adding new Employee detail, Modifying Employee detail, Removing Employee detail, these are the related functions. Instead of creating EmpAddAction, EmpModifyAction , EmpDelAction each with execute method, by using dispatch action, we can create only one action EmpAction class which will have three methods add(), modify(), delete().

How it works ?

DispatchAction manages routing requests to the corresponding method in action class. It decides which method to call based on the value of a request parameter that is passed to it from the incoming request using the request parameter “method”.

To configure the use of this action in your struts-config.xml file, create an entry like this:


<action-mappings>

<action name="empForm" path="/emp" scope="request" type="employee.EmpAction" validate="true" parameter="<strong>method</strong>">

<forward name="add" path="/output.jsp">

<forward name="del" path="/output.jsp"></forward>

<forward name="modify" path="/output.jsp"></forward>

</action>

</action-mappings> 

You might have the following three methods in the same action:

 

The skeleton of the Dispatch action is given below


package employee;

public class EmpAction extends DispatchAction {

public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {}

public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {}

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {}

} 

We can call the method as follows

http://localhost:8080/test/Emp.do?method=modify

 

Reference : Struts DispatchAction

You may also like

Leave a Reply