DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

DispatchAction Functionality in Struts 2 Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (0)
Save
Tweet
68.71K Views

Join the DZone community and get the full member experience.

Join For Free

In Struts 1 DispatchAction helps us in grouping a set of related functions into a single action. In Struts 2 all the Actions by default provide this functionality. To use this functionality we need to create different methods with the similar signature of the execute() method, only the name of the method changes.

In our example the UserAction class contains all the functions related to a User like addUser(), updateUser() and deleteUser().

package vaannila;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	private String message;

	public String execute()
	{
		message = "Inside execute method";
		return SUCCESS;
	}

	public String add()
	{
		message = "Inside add method";
		return SUCCESS;
	}

	public String update()
	{
		message = "Inside update method";
		return SUCCESS;
	}

	public String delete()
	{
		message = "Inside delete method";
		return SUCCESS;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

Unlike Struts 1 you can even have the execute() method along with the other methods in the Action class. We need to specify which method in the action class will be called while configuring the action mapping. A separate action mapping needs to be created for each method in the action class. The following action mapping is done in the struts.xml file.

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

<struts>
    <package name="default" extends="struts-default">
        <action name="User" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="addUser" method="add" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="updateUser" method="update" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="deleteUser" method="delete" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Note that we use the same action class in all the action mappings. When the request URL is "User" the execute() method in the UserAction class will be invoked. When the request URL is "addUser" the add() method in the UserAction class will be invoked, this is specified using the method attribute of the action tag. Similarly for update and delete request the updateUser() and deleteUser() methods will be invoked respectively.

Configuring a seperate action mapping for each method in the action class can be avoided by using an another feature of Struts 2 called Dynamic Method Invocation. The next example explains how to do this. ( Dynamic Method Invocation Example)

In the index.jsp page we create four different buttons to invoke the different methods in the UserAction class. The submit tag is used to create the buttons in the jsp page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="User" >
    <s:submit />
    <s:submit action="addUser" value="Add" />
    <s:submit action="updateUser" value="Update" />
    <s:submit action="deleteUser" value="Delete" />
</s:form>
</body>
</html>

Now lets execute the example. On running the example the following page will be displayed to the user. When the user click a button the appropriate method in the UserAction class gets invoked.

When the user clicks the Add button the addUser() method in the UserAction class gets executed and the following page is displayed to the user.

You can download this example by clicking the Download link below.

Source :Download
War :Download
Requests Download WAR (file format) Links Attribute (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why I'm Choosing Pulumi Over Terraform
  • Why Great Money Doesn’t Retain Great Devs w/ Stack Overflow, DataStax & Reprise
  • 10 Books Every Senior Engineer Should Read
  • Modern REST API Design Principles and Rules

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo