JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







DispatchAction
  • 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.

Example:-
In our example the UserAction class contains all the functions related to a Employee  like
addEmp(), updateEmp() and deleteEmp().


EmployeeAction
.java

package myaction;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction 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.

Now We need to specify which method in the action class will be called
while configuring the action mapping.

struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="Employee" class="myaction.EmployeeAction">
 <result name="success">/success.jsp</result>
 </action>
 <action name="addEmp" method="add"
class="myaction.EmployeeAction">
 <result name="success">/success.jsp</result>
 </action>
 <action name="updateEmp" method="update"
class="myaction.EmployeeAction">
 <result name="success">/success.jsp</result>
 </action>
 <action name="deleteEmp" method="delete"
class="myaction.EmployeeAction">
 <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>

  • When the request URL is "Employee" the execute() method in theEmployeeAction class will be invoked.
  • When the request URL is "addEmp" the add() method in the EmployeerAction class will be invoked, this is specified using the method attribute of the action tag.
In the index.jsp page we create four different buttons to invoke the different methods in
the EmployeeAction class

<%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="Employee" >
 <s:submit />
 <s:submit action="addEmp" value="Add" />
 <s:submit action="updateEmp" value="Update" />
 <s:submit action="deleteEmp" value="Delete" />
</s:form>
</body>
</html>

success.jsp

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<h3><s:property value="message"/></h3>
</body>
</html>

Output:
  • On running the example the following page will be displayed to the user.
  •  When the user click a button the appropriate method in the
    EmployeeAction class gets invoked.








Download Source without lib
Download War with lib


Dynamic Method Invocation.

  • 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.
  • We can avoid configuring a seperate action mapping for each method in the Action
    class by using the wildcard method.
  • The Previous Example can be done through this way.
  • we only change the struts.xml file as shown below.:-


<?xml version="1.0" encoding="UTF-8"?>
<!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="*Employee" method="{1}"
class="myaction.EmployeeAction">
 <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>
  • we have replaced all the method names with an asterisk symbol.
  • . The word that matches for the first asterisk will be substituted for the method attribute. 
  • So when the request URL is "addEmp" the add() method in the EmployeeAction class will be
    invoked.


<%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="Employee" >
 <s:submit />
 <s:submit action="addEmployee" value="Add" />
 <s:submit action="updateEmployee" value="Update" />
 <s:submit action="deleteEmployee" value="Delete" />
</s:form>
</body>
</html>

Download Source without lib
Download War with lib