======================DispatchAction======================
This is an abstract Action that provides user defined methods rather than standard "execute()"
method of Action class. The method name to execute comes as request parameter (may be from hidden
field or JSP page). This Action is useful for developers who prefer to combine many similar actions
into a single Action class, in order to simplify their application design.
Ex: Suppose you have 3 operations "Update, Insert, Delete" then basically you need to write
3 Action classes UpdateEmpAction, InsertEmpAction and DeleteEmpAction having "execute()" method in
each class causing duplicacy of codes.
Using DispatchAction you need to write only one Action class with 3 different methods.
To configure the use of this action in our struts-config.xml file, create an entry like this:
<action path="/saveSubscription"
type="org.apache.struts.actions.DispatchAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="method">
<forward name="insert-success" path="insertEmpSuccess.jsp" redirect="true"/>
<forward name="delete-success" path="deleteEmpSuccess.jsp" redirect="true"/>
<forward name="update-success" path="updateEmpSuccess.jsp" redirect="true"/>
</action>
This configuration uses the value of the request parameter named "method" to pick the
appropriate method in DispatchAction, which must have the same signature of the standard Action.execute().
For example, we should have the following three methods in the same action:
===========*********============
public class EmpAction extends DispatchAction {
Public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("update-success");
}
Public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("insert-success");
}
Public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("delete-success");
}
}
===========********=============
Call to this Action will be done using the URL below in "subscription.jsp":
http://localhost:8080/myapp/saveSubscription.do?method=update
http://localhost:8080/myapp/saveSubscription.do?method=insert
http://localhost:8080/myapp/saveSubscription.do?method=delete
Also the same thing can be done by passing parameter as hidden field (parameter attribute in action tag):
<html:hidden property="method" value="update"/>
<html:hidden property="method" value="insert"/>
<html:hidden property="method" value="delete"/>
Rather than having a single execute() method, we have a method for each logical action.
The DispatchAction dispatches to one of the logical actions represented by the methods.
Monday, January 10, 2011
DispatchAction in Struts
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment