Struts MappingDispatchAction

Using MappingDispatchAction class, you can write multiple related functions in into one Action class and removes the creation of multiple action classes. But unlike DispatchAction, you have to write different various handlers for each function in struts-config.xml.

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 MappingDispatchAction

Create a class CalculatorAction.java which extends MappingDispatchAction 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.MappingDispatchAction;
 
public class CalculatorAction extends MappingDispatchAction{
    public ActionForward addMethod(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 subtractMethod(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 multiplyMethod(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 divideMethod(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="/add" type="com.kruders.action.CalculatorAction"  parameter="addMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
 
<action  path="/subtract" type="com.kruders.action.CalculatorAction" parameter="subtractMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
  
<action  path="/multiply" type="com.kruders.action.CalculatorAction" parameter="multiplyMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
  
<action  path="/divide" type="com.kruders.action.CalculatorAction"   parameter="divideMethod">
    <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>Login Page</title>
</head>
<body>
    
<html:link action="/add.do?numberA=10&numberB=5">Add</html:link>
<html:link page="/subtract.do?numberA=10&numberB=5">Subtract</html:link>
<html:link page="/multiply.do?numberA=10&numberB=5">Multiply</html:link>
<html:link page="/divide.do?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="/add" type="com.kruders.action.CalculatorAction"  parameter="addMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
 
<action  path="/subtract" type="com.kruders.action.CalculatorAction" parameter="subtractMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
  
<action  path="/multiply" type="com.kruders.action.CalculatorAction" parameter="multiplyMethod">
    <forward name="success" path="/calculator.jsp"/>
</action>
  
<action  path="/divide" type="com.kruders.action.CalculatorAction"   parameter="divideMethod">
    <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 6.1

Figure 6.1 Figure 6.1


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

Figure 6.2 Figure 6.2

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

Figure 6.3 Figure 6.3

You can download the source code of this example here.

One Response to Struts MappingDispatchAction

  1. Bhadresh May 22, 2013 at 7:08 am #

    Great & Simple Example!

    Keep Posting Like This…!

    Thanks A Ton!

Leave a Reply