Struts DispatchAction Tutorial

Overview :

DispatchAction provides a mechanism for grouping a set of related functions into a single action, instead of writing separate actions for each functions.


In this example we will see how to group a set of user related actions like insert user, update user,save user and search user into a single action class UserAction.


The use DispatchAction we need to extend our Action class to org.apache.struts.actions.DispatchAction.In the struts 1.3 release the DispatchAction has been move to a new jar struts-extras-1.3.10.So you need to add this jar file to your project class path.This jar file is already included in example for this tutorial.Download the Example and import the ready made project into eclipse IDE.


UserAction class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute() method to manage delegating the request to the individual methods based on the incoming request parameter.

Overview :

DispatchAction provides a mechanism for grouping a set of related functions into a single action, instead of writing separate actions for each functions.


In this example we will see how to group a set of user related actions like insert user, update user,save user and search user into a single action class UserAction.


The use DispatchAction we need to extend our Action class to org.apache.struts.actions.DispatchAction.In the struts 1.3 release the DispatchAction has been move to a new jar struts-extras-1.3.10.So you need to add this jar file to your project class path.This jar file is already included in example for this tutorial.Download the Example and import the ready made project into eclipse IDE.


UserAction class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute() method to manage delegating the request to the individual methods based on the incoming request parameter.






let us start with our example

Step 1: Create JSPs for UI of the application :

1. index.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> 
<html:html>
<HEAD>
<TITLE>Dispatch Action Example</TITLE>
<BODY>

<H3>Dispatch Action Example</H3>
<p><html:link page="/user.do?parameter=add">Call Add Section</html:link></p> 
<p><html:link page="/user.do?parameter=edit">Call Edit Section</html:link></p> 
<p><html:link page="/user.do?parameter=search">Call Search Section</html:link></p> 
<p><html:link page="/user.do?parameter=save">Call Save Section</html:link></p> 

</html:html>

In index.jsp, we have created links using tag and passed a parameter "parameter" with values add, edit, search etc. This value is fetched by the Dispatch Action and is used to call corresponding method in Action class. Thus if we click Edit User link, a parameter edit will be passed to the action class and corresponding edit() method will be called by framework.

2. adduser.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
	<title>Dispatch Action Example</title>
</head>
<body>
<h2>Welcome to Add Section</h2>
</body>
</html>
3. edituser.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
	<title>Dispatch Action Example</title>
</head>
<body>
<h2>Welcome to Edit Section</h2>
</body>
</html>
4. searchuser.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
	<title>Dispatch Action Example</title>
</head>
<body>
<h2>Welcome to Search Section</h2>
</body>
</html>
5. saveuser.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
	<title>Dispatch Action Example</title>
</head>
<body>
<h2>Welcome to Save User Section</h2>
</body>
</html>

Step 2: Create DispatchAction class :

Create a java class UserAction.java and add below line of codes into this file.

UserAction.java
package com.jwt.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 UserAction extends DispatchAction

{

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception

	{

		return mapping.findForward("edit");

	}

	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("search");

	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("save");

	}

}

In above code, we have created separate methods (add(), edit(), search() and save()) for each functionality. Also note that the method signature of these methods are exactly similar to the execute() method of Action class file.

Step 3: Developing the FormBean Class :

Our form bean class contains only one property "parameter" which is playing prime role in this example. Based on the parameter value appropriate function of Action class is executed. Here is the code for FormBean.

UserForm.java
package com.jwt.form;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private String parameter = " ";

	public String getParameter()

	{

		return parameter;

	}

	public void setParameter(String parameter)

	{

		this.parameter = parameter;

	}

}


Step 4: Create a mapping for action and form bean in struts-config.xml file:

UserForm.java
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="DispatchActionForm" type="com.jwt.form.UserForm" />
	</form-beans>

	<action-mappings>
		<action path="/user" type="com.jwt.action.UserAction"
			parameter="parameter" input="/index.jsp" name="DispatchActionForm"
			scope="request" validate="false">
			<forward name="add" path="/adduser.jsp" />
			<forward name="edit" path="/edituser.jsp" />
			<forward name="search" path="/searchuser.jsp" />
			<forward name="save" path="/saveuser.jsp" />
		</action>
	</action-mappings>

</struts-config>

We have added an extra attribute in <action> tag, parameter="parameter" . DispatchAction will read a request parameter called "parameter" and its value will decide the method to be called. Suppose you have a request parameter "parameter" with value "add", Dispatch Action will call add() method from your Action file.

Struts Application in Eclipse

Copy all the tld files to WEB-INF folder and make the entry of these tld files in web.xml file.

After that we have to configure ActionServlet of struts with web.xml.The first page that will be called in the login application is the login.jsp page. This configuration should be done in web.xml as <welcome-file-list> . The following xml shows how to configure struts in web.xml.

Step 5 : Configure web.xml

Now we have to configure ActionServlet of struts with web.xml. The following xml shows how to configure struts in web.xml.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<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>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>

Run the Application :


Type http://localhost:9999/DispatchActionExample/index.jsp in your browser,you can get output as shown below.

Struts Dispatch Action

Struts Dispatch Action

Struts Dispatch Action

Directory Structure Of the Project

Directory Structure of the project is given below.

Struts Dispatch Action

You can download the source code of the example by clicking on the Download link below.

Download this example(src+lib) developed in eclipse




comments powered by Disqus