2012-06-13 8 views
1

여기에 설치가 있습니다. gwt 2.4와 gwt-platform 0.7을 사용하고 있습니다. 키 - 값 - 쌍 (순간에 int-> String)을 포함하는 클래스가 있습니다. JPA를 통해 데이터베이스의 다른 테이블에 저장되기 때문에 그들은 단지 다른 클래스입니다.발송을 통해 java.lang.Class를 보내는 가장 좋은 방법은 무엇입니까?

이제 서버에서이 데이터를 가져 오는 (!) 메서드를 하나 갖고 싶습니다.

나는 먼저 ArrayList<Class<?>>을 사용하여 가져 오려는 클래스를 서버에 보내려고했습니다. HashMap<Class<?>, HashMap<Integer, String>>으로 대답하십시오. 그러나 GWT는 Class<?>을 직렬화 할 수 없습니다. 이 방법으로 모든 데이터베이스 항목을 가져 와서 중요한 클래스와 관련된 적절한 클래스를 표시 할 수 있습니다.

이제 많은 코드를 작성하지 않고도 작동하도록 다른 방법을 찾고 있습니다.

첫 번째 새로운 아이디어는 shared 폴더 안의 HashMap<String, Class<?>> 어딘가에 있고 문자열을 전송하는 것입니다. 따라서 클라이언트와 서버는 HashMap의 String을 통해 클래스를 찾아서 새로운 객체를 생성해야합니다.

다른 좋은 해결책이 있습니까?

감사합니다.

답변

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(); 
     } 
    } 
    }); 

그렇게하는 방법입니다.