2012-06-13 121 views
1

这里是设置:我使用GWT 2.4和gwt-platform 0.7。我有一堆包含键值对的类(此刻int-> String)。它们只是不同的类,因为它们通过JPA被保存到数据库中的不同表中。通过派送发送java.lang.Class的最佳方式是什么?

现在我想有一个(!)方法从服务器获取这些数据。

我首先尝试使用ArrayList<Class<?>>向服务器发送我想要获取的类。并用HashMap<Class<?>, HashMap<Integer, String>>回答。但GWT不允许序列化Class<?>。这样我就可以获得所有的数据库条目,并且可以非常方便地与正确的类关联(这很重要)显示它们。

现在我正在寻找另一种方式来让它工作,而无需编写大量的代码。

第一个新想法是HashMap<String, Class<?>>shared文件夹中的某个位置,并通过电线传输字符串。因此,客户端和服务器必须通过HashMap中的字符串查找类来创建新对象。

有没有其他的好办法呢?

谢谢。

回答

0
public Enum ClassType { 
    A, B, C 
} 

public class AType { 
    HashMap<Integer, String> myHashMap; 
    ClassType getClassType() { 
     return ClassType.A; 
    } 
} 

public interface TransferableHashMap extends IsSerializable { 
    ClassType getClassType(); 
} 

public interface transferService extends RemoteService { 
    HashSet<TransferableHashMap> getMaps(HashSet<ClassType> request); 
} 


//somewhere on the client 
final Set<AType> as = new Set<AType>(); 
final Set<BType> bs = new Set<BType>(); 
final Set<CType> cs = new Set<CType>(); 

Set<ClassType> request = new HashSet<ClassType>(); 
request.add(ClassType.A); 
request.add(ClassType.B); 
request.add(ClassType.C); 

transferService.getMaps(request, 
    new AsyncCallback<HashSet<TransferableHashMap>>(){ 

    @Override 
    public void onSuccess(HashSet<TransferableHashMap>> result) { 
     for (TransferableHashMap entry : result) { 
     if(entry instanceof Atype) as.add((AType)entry); 
     else if(entry instanceof Btype) bs.add((BType)entry); 
     else if(entry instanceof Ctype) cs.add((CType)entry); 
     else throw new SerializationException(); 
     } 
    } 
    }); 

这就是我该怎么做的。

相关问题