java4all@1986 java. Powered by Blogger.

Struts Dispatch Action Example?

>> Monday, June 20, 2011





Dispatch Action 
               DispatchAction provides a mechanism for grouping a set of related functions into a single action,
thus eliminating the need to create seperate actions for each functions. In this example we will see
how to group a set of user related actions like add number , subtract number , multiply number and
dividing numbers  into a single action called UserAction.

The class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an
implementation of the execute() method as the normal Action class does. The DispatchAction uses the
execute method to manage delegating the request to the individual methods based on the incoming request parameter.
For example if the incoming parameter is "method=add", then the add method will be invoked. These methods should
have similar signature as the execute method.

Let us look the Example:

Step1:
   Take a jsp named as Calculate.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<title>Insert title here</title>
</head>
<body>
 <h2>Do your Calculations</h2>
 <html:form action="/calc.do">
 <table>
 <tr>
  <td width="60%">
  Enter First Number:<html:text property="fno"></html:text><br>
  Enter Second Number:<html:text property="sno"></html:text><br>
  
  
  <html:submit property="method" value="add">add</html:submit>
  <html:submit property="method" value="sub">sub</html:submit>
  <html:submit property="method" value="mul">mul</html:submit>
  <html:submit property="method" value="div">div</html:submit>
  </td>
  <td><img src="http://www.accountservonline.com/images/calculator_djk3.jpg" height="100" width="150"></img></td> 
 </tr>

 </table>
 </html:form>
 
 </body>
</html>

 Take a FormBean Class i.e CalculateForm.java
package com.bhaskar.dispatch;

import org.apache.struts.action.ActionForm;

public class CalculateForm extends ActionForm{
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private int fno;
 private int sno;
 
 
 public int getFno() {
  return fno;
 }
 public void setFno(int fno) {
  this.fno = fno;
 }
 public int getSno() {
  return sno;
 }
 public void setSno(int sno) {
  this.sno = sno;
 }
 
 

}
Take An Action Class Which extends DispatchAction i.e CalculateAction.java
package com.bhaskar.dispatch;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class CalculateAction extends DispatchAction {
  
 private int a = 0;
 private int b = 0;
 private int c1 = 0;
 Calculate c;
 ArrayList al=null;
 
 public String key="";
 
 public ActionForward add(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a+b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("add", al);
  return maping.findForward("add1");
 }
 
 public ActionForward sub(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  
  a = cf.getFno();
  b = cf.getSno();
  if(a>b){
   c1 = (a-b);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
   
  }
  
  else{
   
   c1 = (b-a);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
  }
  
  return maping.findForward("sub1"); 
 }
 
 public ActionForward mul(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;//It is  a Dto Class
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a*b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("mul", al);
  return maping.findForward("mul1"); 
 }
 
 public ActionForward div(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  if(b==0){
   c1 = 0;
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("div", al);
  }
  else{
  c1 = (a/b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("div", al);
  }
  
  return maping.findForward("div1"); 
 }
}


Take A Dto class DTO means Data Transfer Object(It is Same A bean class)  i.e Calculate.java
package com.bhaskar.dispatch;
public class Calculate {
 private int fno;
 private int sno;
 private int result;
 public int getFno() {
  return fno;
 }

 public void setFno(int fno) {
  this.fno = fno;
 }

 public int getSno() {
  return sno;
 }

 public void setSno(int sno) {
  this.sno = sno;
 }

 

 public int getResult() {
  return result;
 }

 public void setResult(int result) {
  this.result = result;
 }


 

}


Configuring Form uri,actionForm And action Class And Respective Jsp Pages in struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans>
        <form-bean name="Calculator" type="com.bhaskar.dispatch.CalculateForm"/>     
    </form-beans>
    
    <global-exceptions></global-exceptions>

    <global-forwards></global-forwards>
   //If you notice the signature of the add, sub,mul and div  methods are 
similar to the execute method except the name. The next step is to 
create an action mapping for this action handler. The request parameter 
name is specified using the parameter attribute. Here the request 
parameter name is method.

    <action-mappings>
        <action  parameter="method" name="Calculator" path="/calc" scope="session" type="com.bhaskar.dispatch.CalculateAction">
            <forward name="add1" path="/add.jsp" />
            <forward name="sub1" path="/sub.jsp" />
            <forward name="mul1" path="/mul.jsp" />
            <forward name="div1" path="/div.jsp" />
            
        </action>
    </action-mappings>
    <message-resources parameter="application"/>
</struts-config>

First the output looks is shown below

  
add.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="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="add" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>
<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="add" >
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>
</logic:present>
</body>
</html>

sub.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="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="sub" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="sub" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>
mul.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="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="mul" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="mul" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>
div.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="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="div" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="div" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>






OUTPUT AS SHOWN BELOW IMAGES ONE AFTER ONE
 Entering Values Are 5,2
When we click on add button the outout is shown in below image





     When we click on subtract button the output shown below

        
When we click on "mul"  button the output vis shown below

         
When we click on "div"   the output is shown below
    


Read more...

JavaScripy wait while paging loading display image?

<html>
<head>
</head>

<body onLoad="init()">
<div id="loading" style="position:absolute; width:100%; text-align:center; top:300px;"><img src="webpageloading.jpeg" border=0></div>
<script>
var ld=(document.all);

var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;

if (ns4)
 ld=document.loading;
else if (ns6)
 ld=document.getElementById("loading").style;
else if (ie4)
 ld=document.all.loading.style;

function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6||ie4) ld.display="none";
}
</script>
<center><img src="webpageloading.jpeg"></center>
</body>
</html>

 }
  function mousehandler(e){
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
 alert(&quot;right is disabled due some security reasons&quot;);
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
 document.oncontextmenu = mischandler;
 document.onmousedown = mousehandler;
 document.onmouseup = mousehandler;
  &lt;/script&gt;
 &lt;/head&gt;

 &lt;body&gt;
  
 &lt;

Read more...

Disable the right click using javascript?

In this tutorial we are disabling the right click by using javascript
 
                           Copy the code and try it
   
           
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  
    <script language="javascript">
 var isNS = (navigator.appName == "Netscape") ? 1 : 0;
  if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
 
  function mischandler(){
   return false;
 }
  function mousehandler(e){
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
 alert("right is disabled due some security reasons");
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
 document.oncontextmenu = mischandler;
 document.onmousedown = mousehandler;
 document.onmouseup = mousehandler;
  </script>
 </head>

 <body>
  
 </body>
</html>

When we run the code it looks like below









When we use right click it appears like below image









Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP