我一遍又一遍地在 Google 上搜索,但找不到任何有关在缓存中存储变量的信息。我所看到的与此事相关的任何事情看起来都非常复杂并且没有得到很好的解释。我正在使用 GWT 设计一个非常简单的登录系统,使用一个文本文件,根据用户是否登录或注销分别附加 true 或 false 来编辑用户行。
问题是,当用户刷新窗口时,他们没有注销(因为我不希望他们注销),但应用程序已经丢失了当前的用户变量,因此看起来他们已注销。我想缓存当前用户的用户名,直到他们注销,以便我可以允许用户刷新/关闭选项卡并仍然登录。我正在寻找相当深入的解释,因为我以前几乎没有经验带缓存!如果问题看起来很愚蠢或者解决方案不可能,请告诉我!
最佳答案
设置 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 applications to expire after expected time period , Class Cookies
关于java - 是否可以使用 GWT 在浏览器缓存中存储变量(最好是字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256189/