Tuesday, August 6, 2013

DispatchAction Functionality in Struts 2 Tutorial

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().
01.package vaannila;
02. 
03.import com.opensymphony.xwork2.ActionSupport;
04. 
05.public class UserAction extends ActionSupport{
06. 
07.private String message;
08. 
09.public String execute()
10.{
11.message = "Inside execute method";
12.return SUCCESS;
13.}
14. 
15.public String add()
16.{
17.message = "Inside add method";
18.return SUCCESS;
19.}
20. 
21.public String update()
22.{
23.message = "Inside update method";
24.return SUCCESS;
25.}
26. 
27.public String delete()
28.{
29.message = "Inside delete method";
30.return SUCCESS;
31.}
32. 
33.public String getMessage() {
34.return message;
35.}
36. 
37.public void setMessage(String message) {
38.this.message = message;
39.}
40. 
41.}
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.
01.<!DOCTYPE struts PUBLIC
02."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. 
05.<struts>
06.<package name="default" extends="struts-default">
07.<action name="User" class="vaannila.UserAction">
08.<result name="success">/success.jsp</result>
09.</action>
10.<action name="addUser" method="add" class="vaannila.UserAction">
11.<result name="success">/success.jsp</result>
12.</action>
13.<action name="updateUser" method="update"class="vaannila.UserAction">
14.<result name="success">/success.jsp</result>
15.</action>
16.<action name="deleteUser" method="delete"class="vaannila.UserAction">
17.<result name="success">/success.jsp</result>
18.</action>
19.</package>
20.</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 UserActionclass. The submit tag is used to create the buttons in the jsp page.
01.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.pageEncoding="ISO-8859-1"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<%@taglib uri="/struts-tags" prefix="s" %>
05.<html>
06.<head>
07.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
08.<title>Insert title here</title>
09.</head>
10.<body>
11.<s:form action="User" >
12.<s:submit />
13.<s:submit action="addUser" value="Add" />
14.<s:submit action="updateUser" value="Update" />
15.<s:submit action="deleteUser" value="Delete" />
16.</s:form>
17.</body>
18.</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.

No comments:

Post a Comment