DWR returns an object. dwr returns an object.
Instance address: http://pan.baidu.com/s/1mg1ipbm一sjsreceives the javabeanobject returned by DWR
1. User-Defined Image
Package com.mol.info; Package com.mol.info;
Public class User {public String name; public User (String name ){
This. name = name;
} Public String getName (){
Return name;
} Public void setName (String name ){
This. name = name;
}
} |
2. Java code:
Package com.mol.info; public class UserDWR {
Public User say (){
User user = new User ("YangBang ");
Return user;
}
} |
3. The dwr. xml configuration file. If DWR returns an object, you need to add relevant information about the JavaBean object in this configuration file. As for the servlet configuration in web. xml, no need to talk about it.
<? Xml version = "1.0" encoding = "UTF-8"?> <Dwr>
<Allow>
<Create creator = "new" javascript = "obj">
<Param name = "class" value = "com.mol.info. UserDWR"/>
</Create> <! -- Configure the JavaBean object -->
<Convert converter = "bean" match = "com.mol.info. User"> </convert>
</Allow>
</Dwr> |
4. JS receives the code returned by DWR:
<Script type = "text/javascript"> Function test (){
Obj. say (cb );
} Function cb (data) {// In the return parameter, call the getName () method of JavaBean to obtain the corresponding value.
Document. getElementById ("in"). innerHTML = data. getName ();
} </Script> |
2. JS receives the List returned by DWR, and sets the object (List or, in, stores a java-an object)
1. Java code:
Public List <User> strList (){
User user = new User ("YangBang"); User user1 = new User ("WGR"); ArrayList <User> list = new ArrayList <User> (); list. add (user); list. add (user1); return list;
} |
2. Code of the dwr. xml configuration file:
<Dwr>
<Allow> <create creator = "new" javascript = "list"> <param name = "class" value = "com. fs. listdwr. listDwr "/> <include method =" strList "/> </create> <convert converter =" bean "match =" com. fs. listdwr. user "> <param name =" include "value =" name "/> </convert> </allow>
</Dwr> |
3. JS receives the code returned by DWR:
Function test (){
List. strList (cb );
} Function cb (data ){
For (var I = 0; I <data. length; I ++ ){
Var value = data [I]. name;
Var values = values + value;
Document. getElementById ("in"). innerHTML = values;
}
} |
3. JS receives the Map object returned by DWR (JavaBean object is saved in Map)
<Script type = "text/javascript">
Function test (){
Map. myMapDwr (AB );
} Function AB (data ){
Var str = "";
For (var key in data ){
Alert (key + "," + data [key]);
Var value = data [key];
Str + = value;
}
Document. getElementById ("in"). innerHTML = str;
}
</Script> |