Struts DispatchAction class is used to group similar functionality into a single action, and execute the function depends on the given parameter value. Here’s an example to show the use of DispatchAction.

Download this example – Struts-DispatchAction-Example.zip

1. DispatchAction class

Create a custom DispatchAction class by extends the DispatchAction class, and declares two methods – generateXML() and generateExcel().

MyCustomDispatchAction.java

package com.mkyong.common.action;
 
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 MyCustomDispatchAction extends DispatchAction{
 
	public ActionForward generateXML(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		request.setAttribute("method", "generateXML is called");
 
	        return mapping.findForward("success");
	}
 
	public ActionForward generateExcel(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
 
		request.setAttribute("method", "generateExcel is called");
 
		return mapping.findForward("success");
	}
}

2. Struts configuration

Declares an action mapping “CustomDispatchAction”, with parameter attribute and “action” as its value. The parameter value “action” is used to control which method to call – generateXML() or generateExcel().

struts-config.xml

<?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>
 
	<action-mappings>
 
	 	<action
			path="/CustomDispatchAction"
			type="com.mkyong.common.action.MyCustomDispatchAction"
			parameter="action"
			>
 
			<forward name="success" path="/pages/DispatchExample.jsp"/>
 
		</action>
 
		<action
			path="/Test"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/TestForm.jsp"
			>
		</action>
 
	</action-mappings>
 
</struts-config>

3. View page

In JSP page, the parameter work as following :

1. /CustomDispatchAction.do?action=generateXML will execute the generateXML() method.
2. /CustomDispatchAction.do?action=generateExcel will execute the generateExcel() method.

TestForm.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
</head>
<body>
 
<h2>Struts - DispatchAction Example</h2>
 
<h4>html:link</h4>
<p>
   <html:link action="/CustomDispatchAction.do?action=generateXML" >
           Generate XML File
   </html:link>
   |
   <html:link action="/CustomDispatchAction.do?action=generateExcel" >
           Generate Excel File
   </html:link>
</p> 
 
<h4>a href</h4>
<p>
   <a href="CustomDispatchAction.do?action=generateXML">
           Generate XML File
   </a>
   |
   <a href="CustomDispatchAction.do?action=generateExcel" >
           Generate Excel File
   </a>
</p> 
 
</body>
</html>

DispatchExample.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
</head>
<body>
 
<h2>Struts - DispatchAction Example</h2>
 
<bean:write name="method"/>
 
</body>
</html>

4. Test it

http://localhost:8080/StrutsExample/Test.do

Struts-DispatchAction-1-example

If the “Generate XML File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchAction.do?action=generateXML

Struts-DispatchAction-2-xml-example

If the “Generate Excel File” link is clicked, it will forward to http://localhost:8080/StrutsExample/CustomDispatchAction.do?action=generateExcel

Struts-DispatchAction-3-excel-example
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials