Monday, April 18, 2011

Exapmle of DiapatchAction in Struts2

There is a little bit of difference in DispatchAction Functionality in Struts1.x and Struts2.0.
We would talk about DispatchAction in Struts2.0 since it is somewhat simpler.
Before taking this example I guess  you must be knowing what actually is a DispatchAction if not please go through this,what actually is a DispatchAction and why it is needed is explained in simple words.
http://rathursp.blogspot.com/2011/04/what-is-dispatchaction-in-struts2.html

Guess now you must have little bit of idea about DispatchAction
Its good to start..

we would create a form with multiple buttons on it,such that each button will have different action to perfrom.

index.jsp


<%@ 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>
 
 
struts.xml 
Configure struts.xml
<!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="rathur.UserAction">
            <result name="success">/success.jsp</result>
        </action>      
        <action name="addUser" method="add" class="rathur.UserAction"> 
/*will call the add() declared in pkg rathur.UserAction*/
            <result name="success">/success.jsp</result> 
/*it will check if the add() has returned success then it will go to the success.jsp page*/
        </action>
        <action name="updateUser" method="update" class="rathur.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="deleteUser" method="delete" class="rathur.UserAction">
            <result name="success">/success.jsp</result>
        </action>  
    </package>
</struts>
notice we are having same UserAction for all the action mapping.
when the requested URL is User the execute() of UserAction would be called,
when the requested url is addUser the add() in the UserAction is called.
UserAction.java

package rathur;
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;          /* these return would be checked in struts.xml  */ 
      }
      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;
    }
}



success.jsp

create a success.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>


output





hi if you got the required output please do comment. :-)





2 comments:

  1. hi thanks abhishek rathur,your example really worked

    ReplyDelete
  2. hi thanks ur example really worked

    ReplyDelete

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...