Ajax-based DWR configuration instance

Source: Internet
Author: User

DWR. xml

 

<! Doctype DWR Public <br/> "-// getahead limited // DTD direct Web remoting 1.0 //" <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 = "No parameter and no return value") {<br/> listname. findstr (callbacklistname); <br/>} else if (t = "simple return value") {<br/> listname. findcity (callbacklistname2); <br/>} else if (t = "simple parameter") {<br/> var T = "ABC"; <br/> listname. findbyname (T, callbacklistname3); <br/>} else if (t = "JavaBean") {<br/> listname. finduser (callbacklistname4); <br/>} else if (t = "Return List") {<br/> listname. findlist (callbacklistname5); <br/>} else if (t = "list with parameters returned") {<br/> var STR = "hello "; <br/> listname. strfindlist (STR, callbacklistname6); <br/>} else if (t = "back to map") {<br/> listname. strfindmap (callbacklistname7); <br/>} else {<br/> alert ("no matching "); <br/>}</P> <p> function callbacklistname (data) {<br/> alert ("No parameter, no return value "); <br/>}< br/> // simple return value <br/> function callbacklistname2 (data) {<br/> alert ("simple return value:" + data ); <br/>}</P> <p> // simple parameter <br/> function callbacklistname3 (data) {<br/> alert ("simple parameter: "+ data); <br/>}</P> <p> // return JavaBean <br/> function callbacklistname4 (data) {<br/> alert ("JavaBean:/N" + data. name + data. age); <br/>}</P> <p> // return list <br/> function callbacklistname5 (data) {<br/> alert ("return 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 with parameters returned <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/> // a map with parameters is returned. <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/> <pead> <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/javascri PT "> <! -- <Br/> function getvalue (object) {<br/> var M = object. options [object. selectedindex]. text <br/> document. searchform. HD. value = m; <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <p> Ajax DWR </p> <br/> <Form ID = "searchform "Name =" searchform "> <br/> <select id =" selecta "onchange =" getvalue (this) "> <br/> <option> No parameter and no return value </option> <br/> <option> simple parameters </option> <br/> <option> return JavaBean </option> <br/> <option> return list </option> <br/> <option> with Parameters back to list </option> <br/> <option> back to map </option> <br/> </SELECT> <br/> <input type = "text" id = "HD" name = "HD"/> <br/> <input type = "button" id = "searchbox" value = "Submit" onclick = "calllist () "/> <br/> </form> <br/> </ptml> <br/>

 

Processing class 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 ("Andy Lau", 50 ); <br/> return user; <br/>}< br/> // return list <br/> public list findlist () {<br/> arraylist list = new arraylist (); <br/> User user1 = new user ("Andy Lau", 50 ); <br/> User user2 = new user ("Zhang Xueyou", 40); <br/> User user3 = new user ("Jackie Chan", 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/> // a list is returned with parameters. <br/> public list strfindlist (string test) {<br/> system. out. println (TEST); <br/> arraylist list = new arraylist (); <br/> User user1 = new user ("Andy Lau", 50 ); <br/> User user2 = new user ("Zhang Xueyou", 40); <br/> User user3 = new user ("Jackie Chan", 80); <br/> list. add (TEST); <br/> list. add (user1); <br/> list. add (user2); <br/> list. add (user3); <br/> return list; <br/>}< br/> // a parameter is provided and a list is returned. <br/> public map strfindmap () {<br/> map = new hashmap (); <br/> User user1 = new user ("Andy Lau", 50 ); <br/> User user2 = new user ("Zhang Xueyou", 40); <br/> User user3 = new user ("Jackie Chan", 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/>

 

Entity class 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/>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.