DWR框架Simple实例
【1】用DWR将java.util.Date转换成JS代码;
1)加载二个jar包(dwr.jar和commons-logging.jar).
2)配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" 3 xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 7 8 <servlet> 9 <servlet-name>dwr-invoker</servlet-name> 10 <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>dwr-invoker</servlet-name> 15 <url-pattern>/dwr/*</url-pattern> 16 </servlet-mapping> 17 18 </web-app>
3)配置dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <create creator="new" javascript="JDate"> <param name="class" value="java.util.Date"></param> </create> </dwr>
4)test1.html(jsp也可以)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test1</title> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/JDate.js"> </script> <script type="text/javascript"> var date=1; function init(){ JDate.getYear(load); } function load(date){ alert(date + 1900 + '年'); } </script> </head> <body onload="init()"> This is my HTML page. <br> </body> </html>
5)代码测试
Url:localhost:8080/darTest/dwr/interface/JDate.js(darTest是项目名)
结果:
(typeof的这个'DWR'] =='不确定')this.dwr = {};
如果(typeof运算DWR ['引擎'] =='不确定')的dwr.engine = {};
如果(typeof运算dwr.engine [_mappedClasses'] ==“未定义”)dwr.engine._mappedClasses = {};
如果(窗口['道场'])dojo.provide('dwr.interface.JDate');
如果(typeof运算['JDate'] ==“未定义”)JDate = {};
JDate._path ='/ dwrTest / DWR';
JDate.equals =函数(P0,回调){
返回dwr.engine._execute(JDate._path,'JDate','等于',参数);
};
JDate.toString =函数(回调){
返回dwr.engine._execute(JDate._path,'JDate','的toString',参数);
};
JDate.hashCode =函数(回调){
返回dwr.engine._execute(JDate._path,'JDate','的hashCode',参数);
};
JDate.clone =函数(回调){
返回dwr.engine._execute(JDate._path,'JDate','克隆',参数);
};
JDate.compareTo =功能(P0,回调){
返回dwr.engine._execute(JDate._path,'JDate','的compareTo',参数);
};
JDate.compareTo =功能(P0,回调){
返回dwr.engine._execute(JDate._path,'JDate','的compareTo',参数);
};
Url:localhost:8080/darTest/test1.html
结果:
2013年
【2】用DWR将JavaBean(基本类型或String类型)转换成JS代码;
1)加载二个jar包(dwr.jar和commons-logging.jar).
2)配置web.xml(同上)
3) Demo2.java(JavaBean编写)
package com.wt.dwr;
public class Demo2 {
public String[] stringArray(){
String[] str={"你","好","dwr"};
return str;
}
}
4)配置dwr.xml
<create creator="new" javascript="SArray"> <param name="class" value="com.wt.dwr.Demo2"></param> </create>
5)test3.html(jsp也可以)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test1</title> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/SArray.js"> </script> <script type="text/javascript"> function init(){ SArray.stringArray(load); } function load(date){ alert(date[2]); } </script> </head> <body onload="init()"> This is my HTML page. <br> </body> </html>
6)代码测试
Url:localhost:8080/darTest/dwr/interface/SArray.js(darTest是项目名)
结果:
(typeof的这个'DWR'] =='不确定')this.dwr = {};
如果(typeof运算DWR ['引擎'] =='不确定')的dwr.engine = {};
如果(typeof运算dwr.engine [_mappedClasses'] ==“未定义”)dwr.engine._mappedClasses = {};
如果(窗口['道场'])dojo.provide('dwr.interface.SArray');
如果(typeof的这个'SArray'] ==“未定义”)SArray = {};
SArray._path ='/ dwrTest / DWR';
SArray.stringArray =函数(回调){
返回dwr.engine._execute(SArray._path,'SArray','字符串数组',参数);
};
Url:localhost:8080/darTest/test3.html
结果:
dwr
【3】用DWR将JavaBean(自定义类型)转换成JS代码;
1)加载二个jar包(dwr.jar和commons-logging.jar).
2)配置web.xml(同上)
3)JavaBean代码
package com.wt.entity; public class Book { private String name; private int count; private double price; public Book(String name,int count,double price){ this.count=count; this.name=name; this.price=price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } ---------------------------------------------------------------------- package com.wt.dwr; import com.wt.entity.Book; public class Demo3 { public Book[] getBook(){ Book[] books=new Book[3]; books[0]=new Book("name1",23,21.1); books[1]=new Book("name2",24,22.2); books[2]=new Book("name3",25,23.3); return books; } }
4)配置dwr.xml
<create creator="new" javascript="GBook"> <param name="class" value="com.wt.dwr.Demo3"></param> </create> <convert match="com.wt.entity.Book" converter="bean"> <param name="include" value="name,count,price"> </param> </convert>
5)test5.html(jsp也可以用)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test1</title> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/interface/GBook.js"> </script> <script type="text/javascript"> function init(){ GBook.getBook(load); } function load(date){ for(var i=0;i<3;i++){ alert(date[i].name+','+date[i].count+''+date[i].price); } } </script> </head> <body onload="init()"> This is my HTML page. <br> </body> </html>
6)测试代码
略: