简体   繁体   English

在gwt的sessionstorage中存储对象

[英]Storing Objects in sessionstorage in gwt

Related questions have been asked multiple times, but I feel that my case is a bit different. 相关问题已被问过多次,但我觉得我的情况有些不同。

Background- 背景-

I am making my application in GWT and I wish to make a module whose object I wish to store in session. 我正在GWT中进行应用程序开发,并且希望创建一个模块,其对象希望存储在会话中。 This module will be responsible to keep the persistent data in it updated by making asynchronous calls to server. 该模块负责通过对服务器进行异步调用来保持其中的持久性数据更新。 I would have a timer which would fire an event to make server calls every specified time. 我将有一个计时器,该计时器将触发一个事件以在每个指定的时间进行服务器调用。 Now, I would get this persistent data from the data structure inside this object from anywhere in my code by getting a reference to this object from session. 现在,我将从代码中任何位置从对象内部的数据结构中获取此持久性数据,方法是从会话中获取对该对象的引用。 So this java script module would basically be an interface between the server and native client java script, which would be responsible for maintaining the data structure. 因此,此Java脚本模块基本上是服务器和本机客户端Java脚本之间的接口,该接口将负责维护数据结构。

Now, I have two problems (as of yet) 现在,我有两个问题(截至目前)

  • sessionStorage stores all the data in key, value pairs where both of them have to be string literals. sessionStorage将所有数据存储在键,值对中,这两个值都必须是字符串文字。 Is there anyway I can store my module object in sessionStorage in a way what it preserves my data? 无论如何,我可以将模块对象保存在sessionStorage中的方式与保存数据的方式相同吗?

  • Even if I find a way to store my module object in sessionStorage , I don't think it can make async calls to server to keep the persistent data up-to-date. 即使我找到了一种将模块对象存储在sessionStorage ,我也不认为它可以对服务器进行异步调用以使持久性数据保持最新。 Is there any alternative to this design? 此设计还有其他选择吗?

Thanks for your inputs. 感谢您的投入。

You can store any Java object in session as long as it implements Serializable. 您可以在会话中存储任何Java对象,只要它实现了Serializable。

The second question is not clear. 第二个问题尚不清楚。 What stops you from making async calls to server? 是什么阻止您对服务器进行异步调用?

If sessionStorage stores key value pairs in string literals then use JSON or XML to communicate between client and server. 如果sessionStorage以字符串文字形式存储键值对,则使用JSON或XML在客户端和服务器之间进行通信。 Finally create the object back from JSON or XML. 最后,从JSON或XML创建对象。

How to make make a async calls to server from the GWT client side? 如何从GWT客户端对服务器进行异步调用?

  • Use AsyncCallback to make a async call to server from GWT client side code. 使用AsyncCallback从GWT客户端代码向服务器进行异步调用。

  • Use GWT JSNI to make a call from JavaScript to Java then use first option. 使用GWT JSNI进行从JavaScript到Java的调用,然后使用第一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM