DWR returns an object. dwr returns an object.

Source: Internet
Author: User

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>


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.