DWR传输和接收对象

以前一直以为DWR只能接收和传输字符串的,后来看了《疯狂AJAX讲义》后,恍然大悟。Convert不得不说,是一个好东东啊!!!!

首先,使用DWR,需配置dwr.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

<dwr>
  <allow>
    <create creator="new" javascript="MyDWR">
      <param name="class" value="com.DWRTest.Controller.MyDWR"/>
    </create>
    <convert converter = "bean" match = "com.DWRTest.model.*"/>
  </allow>
</dwr>


我们配置com.DWRTest.Controller.MyDWR,且配置com.DWRTest.model下的所有文件一个转换器。这个转换器,才使得DWR能够传递及访问对象。

假设model下有一个POJO:

package com.DWRTest.model;

public class User {

	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

那么可以做以下几个实验:

1.DWR获得JAVA传递的对象

java代码:

public User getMyUser() {
		
		User user = new User();
		user.setUsername("tammypi");
		user.setPassword("1988");
		
		return user;
	}


利用javascript访问这个对象:

MyDWR.getMyUser(function(user){
	
		alert(user);
		alert('用户名:' + user.username + ",密码:"+user.password);
}


2.DWR获得JAVA传递的对象List:

java代码:

public List<User> getMyUserList() {
		
		List<User> list = new ArrayList<User>();
		
		User user2 = new User();
		user2.setUsername("tammypi");
		user2.setPassword("1988");
		
		User user1 = new User();
		user1.setUsername("tony");
		user1.setPassword("1962");

		list.add(user1);
		list.add(user2);
		
		return list;
	}


利用javascript访问list:

MyDWR.getMyUserList(function(list){
		
			for(var i=0;i<list.length;i++) {
			
				alert("访问第"+i+"个对象");
				alert(list[i].username+","+list[i].password);
			}
		});


3.DWR传递对象,并访问JAVA返回的Map:

java代码:

public Map<String,String> insertUser(User user){
		
		System.out.println(user.getUsername()+","+user.getPassword());
		
		Map<String,String> map = new HashMap<String,String>();
		map.put(user.getUsername(), user.getPassword());
		
		return map;
	}


利用javascript访问返回的map:

var myUser = new Object();
		myUser.username = "111";
		myUser.password = "777";
		
		MyDWR.insertUser(myUser,function(dat11){
		
			for(var key in dat11) {
			
				alert(key+","+dat11[key]);
			}
		});


4.DWR传递对象数组:

java代码:

public void insertUserList(User[] users){
		
		for(int i=0;i<users.length;i++){
			
			User user = users[i];
			System.out.println("用户名:"+user.getUsername()+",密码:"+user.getPassword());
		}
	}


javascript代码:

var arr = new Array();
		var myUser1 = new Object();
		myUser1.username = "kkdkf";
		myUser1.password = "kj";
		var myUser2 = new Object();
		myUser2.username = "kkdkf113434";
		myUser2.password = "kj343434";
		arr[0]=myUser1;
		arr[1]=myUser2;
		MyDWR.insertUserList(arr);


看来还是要不断的学习啊,以前执着于已经掌握的方法,却从来没有思考过,已经掌握的方法到底好不好。人类之所以脱离于猿猴,就是因为学会了思考,学会了不断发展与进步。写代码也是一样,应该不断的吸取养分,应该不断的进步。即使是重复的劳动,也要不断的改变自己的劳动方式,以使得劳动更有效率!

 

你可能感兴趣的:(JavaScript,java,String,list,user,DWR)