<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>DWRDemo.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type='text/javascript' src='/1102_DWR/dwr/interface/DWRDemo.js'></script> <script type='text/javascript' src='/1102_DWR/dwr/engine.js'></script> <script type='text/javascript' src='/1102_DWR/dwr/util.js'></script> <script type='text/javascript' src='jquery/jquery-1.6.1.js'></script> <script type="text/javascript"> jQuery.noConflict(); /** * 调用后台getArray() * @param {Object} "#bt1" */ jQuery(function(){ jQuery("#bt1").click(function(){ DWRDemo.getArray(function(array){ /* * JS for循环遍历数组 for(var i=0; i<array.length;i++){ alert(array[i]); } */ //JQuery each函数 循环遍历数组 jQuery.each(array,function(i,n){ alert(n); }) }); }); /** * 调用后台getList1() */ jQuery("#bt2").click(function(){ DWRDemo.getList1(function(array){ //JQuery each函数 循环遍历数组 jQuery.each(array,function(i,n){ alert(n); }) }); }); /** * 调用后台getList2() */ jQuery("#bt3").click(function(){ DWRDemo.getList2(function(array){ alert(array); for(var i=0;i<array.length;i++){ for(var j=0;j<array[i].length;j++){ alert(array[i][j]); } } }); }); /** * 调用后台getList3() */ jQuery("#bt4").click(function(){ DWRDemo.getList3(function(array){ alert(array); /** * for(var i=0;i<array.length;i++){ alert(array[i].id); alert(array[i].name); } */ // for in 循环 for(var i in array){ alert(array[i].id); alert(array[i].name); } jQuery.each(array,function(i,stu){ alert(stu.id); alert(stu.name); }) }); }); }) </script> </head> <body> <input type="button" value="getArray()" id="bt1"> <br> <input type="button" value="getList1()" id="bt2"> <br> <input type="button" value="getList2()" id="bt3"> <br> <input type="button" value="getList3()" id="bt4"> <br> </body> </html>
dwr.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="DWRDemo"> <param name="class" value="com.wepull.dwr.DWRDemo"></param> </create> <convert match="com.wepull.dwr.Student" converter="bean"></convert> </allow> </dwr>
后台java
package com.wepull.dwr;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DWRDemo {
public String[] getArray(){
String str[] = {"a","b","c",};
return str;
}
public List<Integer> getList1(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
return list;
}
public List<String[]> getList2(){
List<String[]> list = new ArrayList<String[]>();
String str1[] = {"a","b","c",};
String str2[] = {"c","d","e",};
list.add(str1);
list.add(str2);
return list;
}
public List<Student> getList3(){
List<Student> list = new ArrayList<Student>();
Student s1 = new Student();
Student s2 = new Student();
s1.setId(1);
s1.setName("周立波");
s2.setId(2);
s2.setName("郭德纲");
list.add(s1);
list.add(s2);
return list;
}
public Set<Student> getSet(){
Set<Student> set = new HashSet<Student>();
Student s1 = new Student();
Student s2 = new Student();
s1.setId(1);
s1.setName("周立波");
s2.setId(2);
s2.setName("郭德纲");
set.add(s1);
set.add(s2);
return set;
}
}