Friday 3 February 2012

DISPATCH ACTION DEMO FOR CRUD OPERATIONS

A NOTE ON DISPATCHACTION IN STRUTS1.1

    When we have a number of related methods,
instead of writing action-class for each operation,
we should use DispatchAction. Usually in Action class we
define execute(...) method.But, in DispatchAction you
can have your own methods which should resemble the
signature of the original execute(...) method.
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response);
I am going to explain this DispatchAction with two applications.
The first one deals with databese CRUD operations and the second
one deals with simple arithmetic operations.
when you use this DispatchAction, you should be careful
while providing action mapping. We have to specify one more
attribute. that is 'parameter' which holds the method name which
we want to access.



My database table has three fields ( key, name, place).
My example has the following files.
demoInput.jsp
demoForm.java
Finally, we have demoOutput.jsp for
displaying the result in table form
using jstl for iteration.
I am giving demoAction.java here
=================================================================
package demopack;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
public class demoAction extends DispatchAction

{
public ActionForward addRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{

demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.addRecord(a,b,c);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//------------------------------------------
public ActionForward modifyRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.modifyRecord(a,b,c);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//--------------------------------------------------
public ActionForward removeRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.removeRecord(a);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure")); }
}
//------------------------------------------------------------
public ActionForward showRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a = demoform.getKey();
String b = demoform.getName();
String c = demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector1 = bean1.showRecord(a); // key
request.setAttribute("result",vector1);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//-------------------------------------------------------
public ActionForward showallRecords(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector1 =bean1.showallRecords();
request.setAttribute("result",vector1);
return(mapping.findForward("success")); }
else
{ return ( mapping.findForward("failure"));}
}

In demoOutput.jsp,I have iterated the objects
returned by the methods defined in
demoAction class
===================================================
package demopack;
import java.util.*;
import java.sql.*;
public class querybean
{
Connection connection;
Vector vector1;
public querybean()
{
vector1 = new Vector();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:keynameplace";
connection=DriverManager.getConnection(url);
}
catch(Exception e1)
{ vector1.addElement(""+e1);}
}
//-------------------------------
public Vector addRecord(String a,String b,String c)
{
Statement statement=connection.createStatement();
String sql="insert into table1 values('"+a+"','"+b+"','"+c+"')";
statement.execute(sql);
player player1 = new player();
player1.setKey("added");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
connection.close();
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//--------------
public Vector modifyRecord(String a,String b,String c)
{
Vector vector1 = new Vector();
try
{
Statement statement=connection.createStatement();
String sql="update table1 set table1.name='"+b+"',table1.place='"+c+"' where table1.key='"+a+"' ";
statement.execute(sql);
player player1 = new player();
player1.setKey("modified");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
connection.close();
}catch(Exception e1) {vector1.addElement(""+e1); }
return vector1;
}
//======================
public Vector removeRecord(String a)
{
try
{
Statement statement=connection.createStatement();
String sql="delete from table1 where table1.key='"+a+"' ";
statement.execute(sql);
player player1 = new player();
player1.setKey("removed");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//===========================
public Vector showRecord(String a)
{
try
{
Statement statement=connection.createStatement();
System.out.println ("statement ready");
String sql="select * from table1 where key='"+a+"' ";
System.out.println(sql);
ResultSet rs=statement.executeQuery(sql);
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
player player1 = new player();
player1.setKey(s1);
player1.setName(s2);
player1.setPlace(s3);
vector1.add(player1);
}
}catch(Exception e1) { vector1.addElement(""+e1); }
return vector1;
}
//======================
public Vector showallRecords()
{
try
{
Statement statement=connection.createStatement();
String sql="select * from table1";
ResultSet rs=statement.executeQuery(sql);
while(rs.next())
{
player player=new player();
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
player.setKey(s1);
player.setName(s2);
player.setPlace(s3);
vector1.add(player);
}
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//======================
}
==========================================
The following code is for the player class that we have used above.
package demopack;
public class player
{
String key;
String name;
String place;
public void setKey(String a) { key = a; }
public void setName(String b) { name = b; }
public void setPlace(String c) { place = c; }
//==================
public String getKey() { return key; }
public String getName() { return name; }
public String getPlace() { return place; }
}
how to iterate the collection of objects created in querybean.


=============================================
I tested the code and got correct results.
This demo shows how we use DispatchAction.
It also shows how the action just returns an array ( vector).
It is the job of the output.jsp to display the result as it wants.
The action class should NOT take the responsibilty of how
the result is to be displayed,
If you want to explain the purpose of DispatchAction to a beginner,
I think we should use a very simple example. We need not bring in
things jstl ,jdbc etc.
We said that we use DispatchAction when there are a number of functions
inter-related as in CRUD.
So, why not a simple calcbean ?
For this purpose I have thought of an calcbean having 4 methods
( add, subtract,multiply and divide).
So, I developed dispatchapp with simple
arithmetic operations (addidtion, subtraction,
multiplication and division.
That will better bring out the essence of DispatchAction.
This is given in my next blog.