DispatchAction
Previous | Home | Next |
The DispatchAction class is used to group related action into one class. DispatchAction class is an abstract class. This class extends the Action class. You have a method for each logical action. The value of the the incoming request parameter is the name of the method.
There are following step is required to use DispatchAction :-
- Firstly,Create an action handler class that subclass DispatchAction.
- Create a method to represent each logical related action.
- Create the action mapping in struts-config.xml file where "parameter" attribute to specify the request parameter that carries the name of the method you want to invoke.
- Paas the action a request parameter taht refer ti the method you want to invoke.
Directory Structure of DispatcherActionExample in Struts 1.3 Using MyEclipse IDE

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><bean:message key="label.title" /></title> </head> <body> <h3><bean:message key="label.header" /></h3> <ul> <li><html:link page="/dispatcherAction.do?method=add">Add Number</html:link></li> <li><html:link page="/dispatcherAction.do?method=multiply">Multiply Number</html:link></li> <li><html:link page="/dispatcherAction.do?method=subtract">Subtract Number</html:link></li> </ul> </body> </html>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans/> <action-mappings> <action path="/dispatcher" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/> <action path="/dispatcherAction" type="org.r4r.struts.MyDispatcherAction" parameter="method" > <forward name="add" path="/add.jsp"/> <forward name="multiply" path="/multiply.jsp"/> <forward name="subtract" path="/subtract.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>
package org.r4r.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class MyDispatcherAction extends DispatchAction { public ActionForward add(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("add"); } public ActionForward multiply(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("multiply"); } public ActionForward subtract(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("subtract"); } }
# Resources for parameter 'org.r4r.struts.ApplicationResources' # Project Struts1.3_DispatcherActionExample label.title = Struts 1.3 DispatcherAction example label.header = Struts 1.3 DispatcherAction example
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><bean:message key="label.title" /></title> </head> <body> <h3><bean:message key="label.header" /></h3> <h4>This message is generated by <span style="color: red;"> ADD METHOD</span> using dispatcher action in struts 1.3</h4> </body> </html>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><bean:message key="label.title" /></title> </head> <body> <h3><bean:message key="label.header" /></h3> <h4>This message is generated by <span style="color: red;"> MULTIPLY METHOD</span>using dispatcher action in struts 1.3</h4> </body> </html>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><bean:message key="label.title" /></title> </head> <body> <h3><bean:message key="label.header" /></h3> <h4>This message is generated by <span style="color: red;"> SUBTRACT METHOD</span> using dispatcher action in struts 1.3</h4> </body> </html>

Previous | Home | Next |