DWR之DWR实例(1)

DWR之DWR实例(1)
----------

1.首先向项目中加入dwr.jar文件,这里我们使用maven构建,同时dwr还要依赖commons-logging.jar包,如例(pom.xml):

[html] view plain copy print ?

	4.0.0
	aaa
	bbb
	war
	0.0.1-SNAPSHOT
	bbb Maven Webapp
	http://maven.apache.org
	
		
			junit
			junit
			3.8.1
			test
		
		
		
			org.directwebremoting
			dwr
			3.0.M1
		
		
		
			commons-logging
			commons-logging
			1.1.1
		
	
	
		bbb
	


2.在web.xml文件中增加DWRSevlet的配置,DWR指定映射路径的js(如:带有/dwr)都由这个servlet处理,就像这些路径的js(带有/dwr的路径):

[html] view plain copy print ?   

这两个件都是虚拟文件,并不是真实的文件。

web.xml的配置如下:

[html] view plain copy print ?



	firstDWR

	
		index.jsp
	

	
	
		DWR Servlet
		dwr-invoker
		org.directwebremoting.servlet.DwrServlet
		
		
			debug
			true
		
		
		
			crossDomainSessionSecurity
			false
		
	
	
		dwr-invoker
		/dwr/*
	



3.创建dwr.xml文件(与web.xml在同一个目录):

[html] view plain copy print ?



	
		
			
		
	


4.创建java文件,如:MathDelegate.java文件:

[java] view plain copy print ?
package app;

/**
 * 数值计算
 * @author fuhd
 */
public class MathDelegate {

	/**加法*/
	public int add(int a,int b){
		
		return a+b;
	}
	
	/**减法*/
	public int subtract(int a,int b){
		
		return a-b;
	}
	
	/**乘法*/
	public int multiply(int a,int b){
		
		return a*b;
	}
	
	/**除法*/
	public int divide(int a,int b){
		
		return a/b;
	}
}

5.创建jsp文件(可以是html或其它模板的文件),如:index.jsp :

[html] view plain copy print ?
  1. <html>  
  2.     <head>  
  3.         <title>firstdwrtitle>  
  4.         <script type="text/javascript" src="dwr/interface/MathDelegate.js">script>  
  5.         <script type="text/javascript" src="dwr/engine.js">script>  
  6.         <script>  
  7.             var a = 0;  
  8.             var b = 0;  
  9.             var op = "";  
  10.             function doMath(){  
  11.                 a = document.getElementById("numA").value;  
  12.                 b = document.getElementById("numB").value;  
  13.                 op = document.getElementById("op").value;  
  14.                 if(op == "add"){  
  15.                     MathDelegate.add(a,b,doMathCallback);  
  16.                     op = "+";  
  17.                 }else if(op == "subtract"){  
  18.                     MathDelegate.subtract(a,b,doMathCallback);  
  19.                     op = "-";  
  20.                 }else if(op == "multiply"){  
  21.                     MathDelegate.multiply(a,b,doMathCallback);  
  22.                     op = "*";  
  23.                 }else if(op == "divide"){  
  24.                     MathDelegate.divide(a,b,doMathCallback);  
  25.                     op = "/";  
  26.                 }  
  27.             }  
  28.             var doMathCallback = function(answer){  
  29.                 document.getElementById("resultDiv").innerHTML = "

    " +   

  30.                     "Result: " + a + " " + op + " " + b + " = " + answer + "h1>";  
  31.             };  
  32.         script>  
  33.     head>  
  34.     <body>  
  35.         <span id="resultDiv">span>  
  36.         Please enter two numbers,select an operation,and click the equals button:  
  37.         <br><br>  
  38.         <input type="text" id="numA" size="4"/>  
  39.            
  40.         <select id="op">  
  41.             <option value="add">+option>  
  42.             <option value="subtract">-option>  
  43.             <option value="multiply">*option>  
  44.             <option value="divide">/option>  
  45.         select>  
  46.            
  47.         <input type="text" id="numB" size="4"/>  
  48.            
  49.         <input type="button" value="=" onclick="doMath();"/>  
  50.     body>  
  51. html> 

转自:http://blog.csdn.net/fhd001/article/details/7063281

dwr官网下载地址:http://directwebremoting.org/dwr/downloads/index.html

你可能感兴趣的:(前台,web_dwr)