【问题标题】:Best way to send java.lang.Class via dispatch?通过调度发送 java.lang.Class 的最佳方式?
【发布时间】:2012-06-13 23:28:39
【问题描述】:

设置如下:我使用的是 GWT 2.4 和 gwt-platform 0.7。我有一堆包含键值对的类(目前是 int->String)。它们只是不同的类,因为它们通过 JPA 保存到数据库中的不同表中。

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

我首先尝试使用ArrayList<Class<?>> 将我想获取的类发送到服务器。并回复HashMap<Class<?>, HashMap<Integer, String>>。但是 GWT 不允许序列化 Class<?>。通过这种方式,我可以很容易地获取所有数据库条目并使用相关的正确类(这很重要)显示它们。

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

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

还有其他好的解决办法吗?

谢谢。

【问题讨论】:

    标签: gwt gwt-platform java.lang.class


    【解决方案1】:
    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();
            }
        }
      });
    

    我就是这样做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2018-09-08
      • 2021-08-31
      相关资源
      最近更新 更多