向DWR传递map/list,返回list/map操作方法
1/dwr class
?public List xxxxx(List<Model> list){
??//从前台获得对象数据
??for (Iterator i = list.iterator(); i.hasNext();) {
???Model m = (Model) i.next();//迭代出? model 对象
???System.out.println(m.getUsername()+"-----------"+m.getPassword()); //调用对象方法
??}
??
??//封装对象传到前台
??Map map1 = new HashMap();
??map1.put("username", "zht1");
??map1.put("password", "aaa");
??
??Map map2 = new HashMap();
??map2.put("username", "zht2");
??map2.put("password", "bbb");
??
??list=new ArrayList();
??list.add(map1);
??list.add(map2);
??
??return list;
?}
?
2/dwr 配置文件
<allow>?
?
?<!-- 转换dwr类 显示可调用方法 这里可以换做Spring管理-->
??<create creator="new" javascript="dwrService">
???<param name="class" value="com.dwr.AjaxAction" />
???<include method="xxxxx" />
??</create>
??
??<!-- 转换实体类(后台转换时用到,必须有) -->
??<convert converter="bean" match="com.model.Model" />
?</allow>
?
?<signatures>
??????? <![CDATA[
??import java.util.*;<!-- dwr后台接收参数时用到list/map,必须有 -->?
??import com.model.*;<!-- dwr后台接收参数时用到Test,必须有 -->
??import com.dwr.*;<!-- 引入待转换的dwr类,必须有 -->?
??AjaxAction.xxxxx(List<Test>);<!-- 引入待转换的dwr类方法,必须有 -->
??????? ]]>
?</signatures>
?
3/页面调用
//map对象
??var map = {
???"key1" : {
????username : "user1",
????password : "password1"
???},
???"key2" : {
????username : "user2",
????password : " password2"
???}
??};
?
//list对象
??var list = [ {
???username : "user1",
???password : "password1"
??}, {
???username : "user2",
???password : " password2"
??} ];
?
//dwr向后台传递对象,并解析后台返回的对象
??dwrService.xxxxx(list, function(msg) {
???for ( var i = 0; i < msg.length; i++) {
????//alert(msg[i]);//这里是后天返回的list数组
????for ( var key in msg[i]) {
?????alert(key);//这里是返回list数组中的map数组
?????alert(msg[i][key]);//value
????}
???}
??});
?