Saturday 28 January 2012

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.

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