通常我们会在客户端(GWT应用程序)中将列表或设置对象作为 JsArray 处理。有没有这样的方法来处理Map对象?
例如:
早些时候我的服务器端DTO是
public class CustomDTO {
private Users users;
public CustomDTO(Users users) {
this.users = users;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
}
所以在GWT应用程序中,我有以下代码来获取它
public final class CustomDTO extends JavaScriptObject {
protected CustomDTO() {
}
public native JsArray<UsersOverlay> getUsers() /*-{return this.users;}-*/;
}
但现在我的服务器端DTO如下
public class CustomDTO {
private Users users;
private Map<Employee, List<Taskss>> tasksMap = Maps.newHashMap();
public CustomDTO(Users users, Map<Employee, List<Taskss>> tasks) {
this.users = users;
this.tasks = tasks;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public List<Tasks> getTasks() {
return tasks;
}
public void setTasks(List<Tasks> tasks) {
this.tasks = tasks;
}
}
所以我现在在客户端有以下代码,
public final class CustomDTO extends JavaScriptObject {
protected CustomDTO() {
}
public native JsArray<UsersOverlay> getUsers() /*-{return this.users;}-*/;
public native Map<Employees, JsArray<Tasks>> getTasks() /*-{return this.tasks;}-*/;
}
这是正确的还是有其他方法来处理Map对象?因为如果我这样处理我会得到ClassCastException吗?如果我这样做:
final List<Map<Employees, JsArray<Tasks>> list = new ArrayList();
for (int i = 0; i < List<CustomDTO>.length(); i++) { // where List<CustomDTO> is the response from the server
list.add(List<CustomDTO>.get(i).getTasks());
}
//**Upto this line it works. I got the ClassCastException in the following line**
for (Map<Employees, JsArray<Tasks>> map : list) {
}
请参考以下图片: