Meyyappan has posted 101 posts at DZone. View Full User Profile

DispatchAction Functionality in Struts 2 Tutorial

06.15.2012
| 40586 views |
  • submit to reddit

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
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: