是否可以使用 GWT 在浏览器缓存中存储变量(最好是字符串)?

发布于 2024-12-02 07:09:14 字数 305 浏览 0 评论 0原文

我一遍又一遍地搜索谷歌,但找不到任何关于在缓存中存储变量的信息。我所看到的与此事相关的任何事情看起来都非常复杂并且没有得到很好的解释。我正在使用 GWT 设计一个非常简单的登录系统,使用一个文本文件,根据用户是否登录或注销分别附加 true 或 false 来编辑用户行。

问题是,当用户刷新窗口时,他们没有注销(因为我不希望他们注销),但应用程序已经丢失了当前的用户变量,因此看起来他们已注销。我想缓存当前用户的用户名,直到他们注销,以便我可以允许用户刷新/关闭选项卡并仍然登录。我正在寻找相当深入的解释,因为我以前几乎没有经验带缓存!如果问题看起来很愚蠢或无法解决,请告诉我!

I have trawled Google over and over but I cannot find anything about storing a variable in the cache. Anything I have seen related to the matter looked very complicated and was not well explained. I am designing a very simple login system with GWT using a text-file that edits a users line by appending true or false depending on whether they are logged in or out respectively.

The problem is that when the user refreshes the window they are not logged out (because I don't want them to be) but the application has lost the current user variables so it appears that they are logged out. I would like to cache the username of the current user until they logout so that I can allow the user to refresh/close the tab and still be logged in. I am looking for quite an in depth explanation as I have little/no previous experience with caching! If the question seems stupid or a solution is not possible please tell me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过气美图社 2024-12-09 07:09:14

设置 Cookie 比这里描述的要容易得多!我只给你一个例子:

    //set a cookie
    final TextBox t1 = new TextBox();
    RootPanel.get().add(t1);
    final TextBox t2 = new TextBox();
    RootPanel.get().add(t2);

    Button b1 = new Button("safe cookie");
    b1.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            // a cookie which expires after one week
            Date now = new Date();
            long nowLong = now.getTime();
            nowLong = nowLong + (1000 * 60 * 60 * 24 * 7);// seven days
            now.setTime(nowLong);
            Cookies.setCookie(t1.getText(), t2.getText(), now);
            //if the time isn't set it expires after the tab closes

        }
    });
    RootPanel.get().add(b1);

    //get a cookie
    final TextBox t3 = new TextBox();
    RootPanel.get().add(t3);
    Button b2 = new Button("get cookie by name");
    b2.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Window.alert(Cookies.getCookie(t3.getText()));

        }
    });
    RootPanel.get().add(b2);

来源:Set Cookies with GWT申请将在预期时间段后过期类 Cookie

Setting Cookies is way easier than described here! I'll just give you an example:

    //set a cookie
    final TextBox t1 = new TextBox();
    RootPanel.get().add(t1);
    final TextBox t2 = new TextBox();
    RootPanel.get().add(t2);

    Button b1 = new Button("safe cookie");
    b1.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            // a cookie which expires after one week
            Date now = new Date();
            long nowLong = now.getTime();
            nowLong = nowLong + (1000 * 60 * 60 * 24 * 7);// seven days
            now.setTime(nowLong);
            Cookies.setCookie(t1.getText(), t2.getText(), now);
            //if the time isn't set it expires after the tab closes

        }
    });
    RootPanel.get().add(b1);

    //get a cookie
    final TextBox t3 = new TextBox();
    RootPanel.get().add(t3);
    Button b2 = new Button("get cookie by name");
    b2.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Window.alert(Cookies.getCookie(t3.getText()));

        }
    });
    RootPanel.get().add(b2);

Sources: Set Cookies with GWT applications to expire after expected time period, Class Cookies

烟雨扶苏 2024-12-09 07:09:14

HTML5 是我能找到的最好、最简单的解决方案。

我使用了 GWT Storage API,可以在 Eclipse 中的此处找到该 API这非常简单。如果其他人想做类似的事情,所有内容都会在我提供的链接中进行解释。

HTML5 was the best and most simple solution that I could find.

I used the GWT Storage API which can be found here in Eclipse and it was extremely simple. If anyone else wants to do a similar thing everything is explained in the link I provided.

游魂 2024-12-09 07:09:14

GWT 不提供任何在浏览器中存储值的选项,但您可以使用“cookie”在浏览器上存储数据。 GWT 将帮助您在浏览器上创建和维护 cookie。如果您计划在浏览器上存储更多数据,请使用 HTML 5 功能。

带有 GWT 的 Google Gear 也是在浏览器上存储数据的一个选项

GWT does't provide any options for storing value in browser but you can use 'cookie' to store data on browser. GWT will help you to create and maintain cookie on browser. If you are planing to store more data on browser use HTML 5 features.

Google Gear with GWT is also an option to store data on browser

旧故 2024-12-09 07:09:14

您可以将数据存储在用户会话中,并通过 RCP 从服务器 onModuleLoad()(或任何您需要的时候)获取数据。从安全角度来看,将登录状态存储在客户端上是一个坏主意,因为他可以将其更改为“已登录”。

You could store the data inside the user session and fetch it from the server onModuleLoad() (or whenever you need it) via RCP. It is a bad idea from a security point of view to store the login state on the client because he could change it to "logged in".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文