如何在 gwt 应用程序中保留计数器哈希图
我正在努力在应用程序级别(而不是数据库)的内存中存储不同的用户计数器,并且计数器最多为 3。
我使用带有 hashmap 的单例类来开发此代码,以存储用户 id 的键值对及其计数器和时间。
初始之后,我在客户端使用这个单例类,它对于计数器工作得很好,但是当刷新浏览器时,会创建一个新的单例对象。
我搜索后发现,出于安全原因,javascript不使用共享对象,因此我将其移至rpc调用并使其成为服务器端对象来克服它,但是当我再次测试它时,刷新时会创建新对象和数据丢失。
我还测试了静态哈希图来保存应用程序范围的数据,但它具有几乎相同的行为,一旦刷新浏览器,其重置和旧数据就会丢失。它的行为与我们遵循这种方法的普通 Java Web 应用程序几乎没有什么不同。
这种技术在正常的 spripng web 应用程序中对我有用,但在 gwt 中不起作用
I am working on storing different user counters in memory at application level (not database) and to have counters to atmost 3.
I developed this code using a singleton class with hashmap to store a key value pair of user id and and it's counter and time.
After initial, i was using this singleton class on client side, it worked fine for counters but when browser was refreshed, a new singleton object was created.
I searched, and come to know that for security reason, javascript don't use shared objects, so i moved it to rpc call and make it server side object to overcome it, but when i tested it again, on refresh new object is created and data lost.
I also tested the static hashmap to hold the data for application scope, but it has almost the same behavior, once refreshed the browser, its reset and old data is lost in it. It little different behavior from normal java web apps where we follow this approach.
this technique used to work for me in case of normal spripng web applicatoins, but not in gwt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是对的,您需要将此值存储在服务器端并通过异步调用访问它。由于您过去使用 Spring 取得了成功,因此您可能会考虑在 GWT 异步服务的实现方面再次使用它。您可以轻松地将 Spring Singleton bean 注入到 GWT Async 实现类中,并像过去一样使用它。
You are correct, you'll need to store this value on the server-side and access it via an Async call. Since you have had success with Spring in the past, you might consider using it again with the implementation side of your GWT Async services. You can easily inject a Spring Singleton bean into the GWT Async implementation class and use it like you have in the past.
计数器的预期寿命是多少?它们是否被其他用户共享?
您可以将它们存储在数据库或会话中,并通过 GWT 异步调用访问它们。
What's the expected life of the counters? Are they shared by other users?
You can store them in the DataBase or in a Session and access them through a GWT Async call.
您可以使用 GWT HTML5 LocalStorage 来获取客户端持久数据,当然使用服务器端。这实际上取决于您想要实现的目标。
You can use GWT HTML5 LocalStorage to have client-side persistent data or of course use the server-side. It really depends on what you are trying to achieve.