Struts DispatchAction

Using DispatchAction class, you can write multiple actions in into one Action class and removes the creation of multiple action classes. This class does not provide an implementation for the execute() method. The DispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter “method“.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
     
     <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-extras</artifactId>
      <version>1.3.10</version>
    </dependency>
     
</dependencies>

1. Create DispatchAction

Create a class CalculatorAction.java which extends org.apache.struts.actions.DispatchAction and write methods to execute your business logic.

package com.kruders.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 CalculatorAction extends DispatchAction{
    public ActionForward add(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA + numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward subtract(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA - numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward multiply(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA * numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
     
    public ActionForward divide(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
 
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA / numberB;
        request.getSession().setAttribute("result", result.toString());
        return mapping.findForward("success");
    }
}

2. Create ActionMapping

Next we create an action mapping for this action handler.

<action path="/calculate" type="com.kruders.action.CalculatorAction"
    parameter="method" validate="false">
    <forward name="success" path="/calculator.jsp"/>
</action>

3. Create JSP Page

Now create the jsp page that delegates requests to different jsp pages.

calculator.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>DispatchAction Page</title>
</head>
<body>
    
<html:link page="/calculate.do?method=add&numberA=10&numberB=5">Add</html:link>
<html:link page="/calculate.do?method=subtract&numberA=10&numberB=5">Subtract</html:link>
<html:link page="/calculate.do?method=multiply&numberA=10&numberB=5">Multiply</html:link>
<html:link page="/calculate.do?method=divide&numberA=10&numberB=5">Divide</html:link>
<<% String result = (String)session.getAttribute("result");   
   if(result!=null) out.println("<br>Result : " + result);%>   
     
</body>
</html>

4. Struts Config

struts-config.xml file contains the following code.

<global-forwards>
    <forward name="calculator" path="/calculator.do"/>
</global-forwards>
<action-mappings>
 
    <action
        path="/calculator"
        type="org.apache.struts.actions.ForwardAction"
        parameter="/calculator.jsp"/>
     
    <action
        path="/calculate"
        type="com.kruders.action.CalculatorAction"
        parameter="method"
        validate="false">
        <forward name="success" path="/calculator.jsp"/>
    </action>
     
</action-mappings>

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file

<welcome-file-list>
    <welcome-file>calculator.jsp</welcome-file>
</welcome-file-list>
 
<servlet>
    <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>

6. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 5.1

Figure 5.1 Figure 5.1


Now when you click on Add link, following screen will be displayed as shown in Figure 5.2

Figure 5.2 Figure 5.2

The folder structure of the example is shown below in Figure 5.3

Figure 5.3 Figure 5.3

You can download the source code of this example here.

No comments yet.

Leave a Reply