dwr.xml
<!DOCTYPE dwr PUBLIC<br /> "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"<br /> "http://www.getahead.ltd.uk/dwr/dwr10.dtd"><br /> <dwr><br /> <allow><br /> <!-- convert converter = "bean" match = "developerworks.ajax.bean.Apartment" --><br /> <create creator = "new" javascript = "ListName"><br /> <param name="class" value= "developerworks.ajax.dao.ListName" /><br /> <!-- include method = "findStr" /><br /> <include method = "findCity" /><br /> <include method = "findUser" /><br /> <include method = "findList" /><br /> <include method = "findByname" /><br /> <include method = "strfindList" /><br /> <include method = "strfindMap" /--><br /> </create><br /> <convert converter="bean" match="developerworks.ajax.bean.User"><br /> <param name="include" value="name,age" /><br /> </convert><br /> </allow><br /> </dwr>
test.js
function calllist(){<br />var t=$("hd").value;<br />if(t=="無參無傳回值"){<br /> ListName.findStr(callBackListName);<br /> }else if(t=="有簡單傳回值"){<br /> ListName.findCity(callBackListName2);<br /> }else if(t=="有簡單參數"){<br /> var t="abc";<br /> ListName.findByname(t,callBackListName3);<br /> }else if(t=="返回javabean"){<br /> ListName.findUser(callBackListName4);<br /> }else if(t=="返回List"){<br /> ListName.findList(callBackListName5);<br /> }else if(t=="有參並返回List"){<br /> var str="hello";<br /> ListName.strfindList(str,callBackListName6);<br /> }else if(t=="返回Map"){<br /> ListName.strfindMap(callBackListName7);<br /> }else{<br /> alert("沒有匹配");<br /> }<br />} </p><p>function callBackListName(data){<br /> alert("無參無傳回值");<br />}<br />//有簡單傳回值<br />function callBackListName2(data){<br /> alert("有簡單傳回值:"+data);<br />}</p><p>//有簡單參數<br />function callBackListName3(data){<br /> alert("有簡單參數:"+data);<br />}</p><p>//返回javabean<br />function callBackListName4(data){<br /> alert("返回javabean:/n"+data.name+data.age);<br />}</p><p>//返回List<br />function callBackListName5(data){<br /> alert("返回List:/n");<br /> var sb="";<br /> for(var i=0;i<data.length;i++){<br /> sb=sb+data[i].name+"+"+data[i].age+"/n";<br /> }<br /> alert(sb);<br />}</p><p>//有參並返回List<br />function callBackListName6(data){<br /> alert(data.length);<br /> var sb="";<br /> sb=sb+data[0]+"/n";<br /> for(var i=1;i<data.length;i++){<br /> sb=sb+data[i].name+"+"+data[i].age+"/n";<br /> }<br /> alert(sb);<br />}<br />//有參並返回Map<br />function callBackListName7(data){<br />alert("callBackListName7");<br /> var sb="";<br /> //sb=sb+data[0]+"/n";<br /> for(var pro in data){<br /> var bean=data[pro];<br /> sb=sb+bean.name+"+"+bean.age+"/n";<br /> }<br /> alert(sb);<br />}
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br /><html><br /><head><br /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /> <title>developerWorks - Ajax with DWR</title><br /> <link rel="stylesheet" type="text/css" href="style.css" mce_href="style.css" media="screen"/><br /> <mce:script type='text/javascript' src="dwr/interface/ListName.js" mce_src="dwr/interface/ListName.js"></mce:script><br /> <mce:script type='text/javascript' src="dwr/engine.js" mce_src="dwr/engine.js"></mce:script><br /> <mce:script type='text/javascript' src="dwr/util.js" mce_src="dwr/util.js"></mce:script><br /> <mce:script type='text/javascript' src="test.js" mce_src="test.js"></mce:script><br /> <mce:script type="text/javascript"><!--<br /> function getvalue(object){<br /> var m=object.options[object.selectedIndex].text<br /> document.searchform.hd.value=m;<br /> }</p><p>// --></mce:script><br /></head><br /><body><br /><h1>Ajax DWR</h1><br /><form id="searchform" name="searchform"><br /> <br/><br /> <select id="selectA" onchange="getvalue(this)"><br /> <option>無參無傳回值</option><br /> <option>有簡單傳回值</option><br /> <option>有簡單參數</option><br /> <option>返回javabean</option><br /> <option>返回List</option><br /> <option>有參並返回List</option><br /> <option>返回Map</option><br /> </select><br /> <input type="text" id="hd" name="hd" /><br/><br /> <input type="button" id="searchbox" value="提交" onclick="calllist()" /><br /></form><br /></html><br />
處理類ListName.java
package developerworks.ajax.dao;</p><p>import java.util.ArrayList;<br />import java.util.HashMap;<br />import java.util.List;<br />import java.util.Map;</p><p>import developerworks.ajax.bean.User;</p><p>public class ListName {</p><p>public ListName() {<br />super();<br />}</p><p>public String findStr(){<br />return "��»�";<br />}</p><p>public String findCity(){<br />return "Beijing";<br />}</p><p>public User findUser(){<br />User user=new User("劉德華",50);<br />return user;<br />}<br />//返回List<br />public List findList(){<br />ArrayList list=new ArrayList();<br />User user1=new User("劉德華",50);<br />User user2=new User("張學友",40);<br />User user3=new User("成龍",80);<br />list.add(user1);<br />list.add(user2);<br />list.add(user3);<br />/*list.add("y1");<br />list.add("y2");<br />list.add("y3");*/<br />return list;<br />}<br />//有參數並返回List<br />public List strfindList(String test){<br />System.out.println(test);<br />ArrayList list=new ArrayList();<br />User user1=new User("劉德華",50);<br />User user2=new User("張學友",40);<br />User user3=new User("成龍",80);<br />list.add(test);<br />list.add(user1);<br />list.add(user2);<br />list.add(user3);<br />return list;<br />}<br />//有參數並返回List<br />public Map strfindMap(){<br />Map map=new HashMap();<br />User user1=new User("劉德華",50);<br />User user2=new User("張學友",40);<br />User user3=new User("成龍",80);<br />map.put("user1", user1);<br />map.put("user2", user2);<br />map.put("user3", user3);<br />return map;<br />}</p><p>public String findByname(String name){</p><p>System.out.println(name);<br />return name+"helloworld";<br />}<br />}<br />
實體類 User.java
package developerworks.ajax.bean;</p><p>public class User {<br />private String name;<br />private int age;<br />public String getName() {<br />return name;<br />}<br />public void setName(String name) {<br />this.name = name;<br />}<br />public int getAge() {<br />return age;<br />}<br />public void setAge(int age) {<br />this.age = age;<br />}<br />public User(String name, int age) {<br />super();<br />this.name = name;<br />this.age = age;<br />}</p><p>}<br />