Saturday 28 January 2012

STRUTS DEMO WITH AJAX USING jQUERY

STRUTS DEMO WITH AJAX USING jQUERY

When you submit the form the page itself is sent to the server. So it took a little bit time. To avoid this we can send the data rather than the whole page. We can provide such functionality to a page with the help of jQuery AJAX. For that we need to do some modifications to our demoInput.jsp page and we don't need demoOutput.jsp since we are going to show the output on the demoInput.jsp page itself. So, let us see how to provide ajax functionality to a jsp page with the help of JQuery. To avoid confussion I rename demoInput.jsp as ajaxdemoInput.jsp and calcapp to ajaxdemoapp.

-----------------------------------------------------------------------

and so we have to do a little bit change in demoAction.java too.

package demopack;


public class demoAction extends Action

{

public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response)throws IOException

{

demoForm demoform=(demoForm)form;

response.setContentType("text/text;charset=utf-8");

response.setHeader("cache-control", "no-cache");

PrintWriter out = response.getWriter();

String a=demoform.getA();

String b=demoform.getB();

String operation=demoform.getOperation();

calcbean bean1=new calcbean();

String r =bean1.calculate(a,b,operation);

String r1 = "Result is ......"+r;

out.println(r1);

out.flush();

return null;

}

//------------------------------------------

}

and calcbean.java and demoForm.java are same as in last example code.

*********************************************************************************

DISPATCHACTION NO NEED AT ALL

DISPATCHACTION NO NEED AT ALL:

calcapp developed in last essay required DispatchAction. But we can modify the bean anddemoAction class inorder to avoid DispatchAction as follows

********************************************

package demopack;

public class calcbean

{

public String calculate

(String s1, String s2,String operation)

{

int a=Integer.parseInt(s1);

int b=Integer.parseInt(s2);

int c;

if(operation.equals("add")) {c=a+b;}

if(operation.equals("subtract")) {c=a-b;}

if(operation.equals("multiply")) {c=a*b;}

if(operation.equals("divide")) {c=a/b;}

return ""+c;

}

}

=====================================================

Here we are passing the method name too as an argument

and so there is no need to write separate function for

each operation. so we can modify the demoAction class

also in the same format,

*******************************************

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 execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

{

demoForm demoform=(demoForm)form;

String a=demoform.getA();

String b=demoform.getB();

String c= demoform.getOperation();

calcbean bean=new calcbean();

String r=bean.calculate(a,b,c);

r="Result is.."+r;

request.setAttribute("result",r);

return (mapping.findForward("success"));

}

}

demoAction class also doesn't need multiple function. Hence there is no necessity for DispatchAction and no need to put aditional entryin mapping.

****************************************************

Suppose you want to retain GUI, you can use aditional control say 'c'( textbox), provide setter/getter method for this property also. Otherwise everything is same as in previous case. but, after calculating set the result to 'c' using setter method of demoForm. I am using a text box to show the result.

so the code for demoAction will be,{

demoForm demoform=(demoForm)form;

String a=demoform.getA();

String b=demoform.getB();

String c=demoform.getOperation();

System.out.println(c);

calcbean bean=new calcbean();

String r= bean.calculate(a,b,c);

demoform.setC(r);

return (mapping.findForward("success"));

**************************************

demoInput.jsp

---------------------------------

When you submit the form the page itself is sent to the server. So it tooks a little bit time. To avoid this we can send the data rather than the whole page. We can provide such functionality to a page with the help of JQuery AJAX. For that we need to do some modifications to our demoInput.jsp page and we don't need demoOutput.jsp since we are going to show the output on the demoInput.jsp page itself in a span

So, let us see how to provide ajax functionality to a jsp page with the help of jQuery.

To avoid confussion I rename demoInput.jsp as ajaxdemoInput.jsp and calcapp to ajaxdemoapp.

**************************************************




SIMPLER EXAMPLE FOR DISPATCHACTION

DISPATCHACTION WITH SIMPLE DEMO


If you want to explain the purpose of DispatchAction to a beginner,
I think we should use a very simple example.

For this purpose I have thought of an calcbean having 4 methods
( add, subtract,multiply and divide).

So, I developed dispatch app with simple
arithmetic operations (addidtion, subtraction,
multiplication and division)This application
has five files ,

1. demoInput.jsp
2. demoAction.java
3. demoForm.java
4. calcbean.java
5. demoOutput.jsp

===============================================


demoAction.java

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 add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;

String a=demoform.getA();
String b=demoform.getB();


if(a.length()!=0)
{
calcbean bean1=new calcbean();
String r =bean1.add(a,b);

String r1 = "sum is ......"+r;



request.setAttribute("result",r1);
}


return(mapping.findForward("success"));
}
…//provided code for subtract , multiply and divide as above
}



calcbean.java

package demopack;

import java.util.*;
import java.sql.*;

public class calcbean
{

public String add(String n1,String n2)
{

int a=Integer.parseInt(n1);
int b=Integer.parseInt(n2);
int c=a+b;

return ""+c;
}

//--------------


…//provided code for subtract , multiply and divide as above


//======================
}
since the calcapp has various functions. One important thing
is action mapping.When we make use of DispatchAction we have to
define the 'parameter' property of tag. For this calcapp
I have provided action mapping as shown below.












========================================================================
We can altogether avoid using DispatchAction, however, even if we have many methods! How?
The simple solution is to add 'operation' also as a parameter to the method in the helperbean.
For examople, we can write our calcbean to have a single method
like calculate(String s1, String s2, String operation)
This will make things very easy.


I have implemented this task in next essay.

Monday 23 January 2012

HOW TO PROVIDE
AJAX SUPPORT FOR A STRUTS PROGRAM USING jQuery?
   Today, I successfully ran a simple demo for using jQuery for Ajax support for Struts. It is very simple and easy.  The idea was obtained from a great   j2ee site www.raistudies.com  
   It is wonderful that many programmers take so much effort to share their knowledge and experience with beginners like me.     When things are explained from practical point of view, they become very easy.
Inspired by such websites, I too am posting what I learn in my labwork in my website.sites.google.com/site/sudharaj4j2ee
Thank you.