2012-03-17 51 views
5

我的GWT應用程序加載時的第一件事是通過RequestFactory從服務器請求當前登錄的用戶。這會阻止,因爲我需要用戶的屬性來知道如何繼續。這隻需要< 500毫秒,但這真的讓我很煩惱,在這段時間內應用程序被阻止。當生成jsp時,我已經在服務器上擁有了用戶,那麼爲什麼不直接將序列化的用戶添加到jsp並完全取消該請求呢?GWT手動序列化服務器上​​的域對象

我有兩個問題讓我從這樣做:

  • 我需要改造用戶到的userProxy
  • 我需要序列化的userProxy的方式,很容易GWT反序列化。

我還沒有想出一個好辦法嗎#1。這種邏輯似乎被埋在ServiceLayerDecorator中,沒有簡單的方法來隔離?我可能在這裏錯了。

第二個似乎通過ProxySerializer容易,但我怎麼得到我的手requestfactory當我在服務器上?您不能在服務器上撥打GWT.create

我一直在尋找到AutoBeans但不處理#1以上。我的UserProxy引用了我想要維護的其他EntityProxy的集合。

回答

1

您不能調用GWT.create服務器(或從任何真正的JVM),但在很多情況下,你可以調用一個JVM兼容的方法爲服務器設計的,而不是使用。在這種情況下,請看RequestFactorySource.create

讓服務器讀取數據並使用RequestFactory打印數據可能有點麻煩 - 下面是一個演示如何工作的示例(使用gwt 2.4,主分支對2.3有相同的功能,或者所以)https://github.com/niloc132/tvguide-sample-parent/blob/gwt-2.4.0/tvguide-client/src/main/java/com/acme/gwt/server/TvViewerJsonBootstrap.java - 與您之前的不完全相同,但可以使用相同的想法在代理存儲中填充字符串,該代理存儲可以在客戶端讀取(請參見https://github.com/niloc132/tvguide-sample-parent/blob/gwt-2.4.0/tvguide-client/src/main/java/com/acme/gwt/client/TvGuide.java)。

的基本思想是(所以代理製作工具可以讓所有正確的片段以一致的方式,包括IDS,調用,並與()參數)來創建一個請求,並把它傳遞到SimpleRequestProcessor實例,那麼這將運行它通過它通常會的服務器部分。 (任何單位的管理制度可能還是應該有緩存,以避免額外的查找實體,否則你需要建模的一些工作SRP的沒有按內部)的ProxySerializer,它包裝一個ProxyStore,預計從發送到有充分的RF消息服務器,所以需要正確完成一小部分消息簿記。

+0

這讓我很遠,但proxySerializer.get(UserProxy.class,UserProxy.STORE_KEY)爲空。但是,proxyStore.get(UserProxy.STORE_KEY)返回一個顯示正確的分割表(至少當它以字符串形式出現時)。它很難追查,因爲這似乎是生成代碼。 – Brad 2012-03-18 01:27:27

+0

大約一年前,我寫了大部分內容(試錯),並且顯然在保持最新狀態方面做得很差 - 我會再試一次以使其更新,查看我失蹤的內容。 – 2012-03-18 01:51:41

3

,如果你能做出User implements UserProxy您可以使用AutoBeans對於這一點。它的工作原理,因爲代理與getter/setter方法的接口:

interface UserFactory implements AutoBeanFactory 
{ 
    AutoBean<UserProxy> user(UserProxy toWrap); // wrap existing instance in an AutoBean 
} 

然後在服務器,你可以創建autobean和序列化到JSON:

UserFactory factory = AutoBeanFactorySource.create(UserFactory.class) 
AutoBean<UserProxy> userProxyBean = factory.user(existingUserPojo); 

// to convert AutoBean to JSON 
String json = AutoBeanCodex.encode(userProxyBean).getPayload(); 

上,您可以只使用AutoBeanCodex客戶端。解碼反序列化JSON回豆

+0

不幸的是'User'不能實現'UserProxy',因爲它具有訪問器,比如'OrganizationProxy getOrganization()',其中返回類型不同,無論我們是代理還是域模型。 – Brad 2012-03-27 14:56:38

+0

誠然,在這種情況下,RF不會讓你這樣做。 – Andrejs 2012-03-27 15:50:09

+0

我不願意承認沒有解決方案:)上面的科林解決方案非常接近。我只是沒有時間深入探索,發現它陷入了哪裏。感謝您的幫助! – Brad 2012-03-27 18:03:29

5

它使用AutoBeans是可能的,如果您爲您的代理的AutoBeanFactory:

  • 要轉換的用戶來的userProxy: 創建一個服務器端RequestFactory和調用同樣的正常請求。響應將包含UserProxy(但在服務器上)。

  • 要序列的userProxy:

    AutoBean<UserProxy> bean = AutoBeanCodex.decode(userAutoBeanFactory, UserProxy.class, json);

  • AutoBean<UserProxy> bean = AutoBeanUtils.getAutoBean(receivedUserProxy);

    String json = AutoBeanCodex.encode(bean).getPayload();

  • 在客戶端反序列化的userProxy

在服務器上創建一個進程RequestFactory(tutorial):

public static <T extends RequestFactory> T create(Class<T> requestFactoryClass) { 
    ServiceLayer serviceLayer = ServiceLayer.create(); 
    SimpleRequestProcessor processor = new SimpleRequestProcessor(serviceLayer); 
    T factory = RequestFactorySource.create(requestFactoryClass); 
    factory.initialize(new SimpleEventBus(), new InProcessRequestTransport(processor)); 
    return factory; 
} 
1

我發現了GWT Google Group答案。所有積分均爲Nisha Sowdri NM

服務器端編碼:

DefaultProxyStore store = new DefaultProxyStore(); 
ProxySerializer ser = requests.getSerializer(store); 
final String key = ser.serialize(userProxy); 
String message = key + ":" + store.encode(); 

客戶端解碼:

String[] parts = message.split(":", 2); 
ProxyStore store = new DefaultProxyStore(parts[1]); 
ProxySerializer ser = requests.getSerializer(store); 
UserProxy user = ser.deserialize(UserProxy.class, parts[0]);