如何重建 GWT 历史堆栈?
我正在使用一个更大的应用程序,但是 GWT 历史文档 有一个简单的示例来演示该问题。 为了方便起见,复制了该示例:
public class HistoryTest implements EntryPoint, ValueChangeHandler
{
private Label lbl = new Label();
public void onModuleLoad()
{
Hyperlink link0 = new Hyperlink("link to foo", "foo");
Hyperlink link1 = new Hyperlink("link to bar", "bar");
Hyperlink link2 = new Hyperlink("link to baz", "baz");
String initToken = History.getToken();
if (initToken.length() == 0)
{
History.newItem("baz");
}
// Add widgets to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.add(lbl);
panel.add(link0);
panel.add(link1);
panel.add(link2);
RootPanel.get().add(panel);
History.addValueChangeHandler(this); // Add history listener
History.fireCurrentHistoryState();
}
@Override
public void onValueChange(ValueChangeEvent event)
{
lbl.setText("The current history token is: " + event.getValue());
}
}
问题是,如果刷新应用程序,历史记录堆栈就会被清除。 如何保留历史记录,以便在用户刷新页面时后退按钮仍然有用?
I have a larger application that I'm working with but the GWT History documentation has a simple example that demonstrates the problem. The example is copied for convenience:
public class HistoryTest implements EntryPoint, ValueChangeHandler
{
private Label lbl = new Label();
public void onModuleLoad()
{
Hyperlink link0 = new Hyperlink("link to foo", "foo");
Hyperlink link1 = new Hyperlink("link to bar", "bar");
Hyperlink link2 = new Hyperlink("link to baz", "baz");
String initToken = History.getToken();
if (initToken.length() == 0)
{
History.newItem("baz");
}
// Add widgets to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.add(lbl);
panel.add(link0);
panel.add(link1);
panel.add(link2);
RootPanel.get().add(panel);
History.addValueChangeHandler(this); // Add history listener
History.fireCurrentHistoryState();
}
@Override
public void onValueChange(ValueChangeEvent event)
{
lbl.setText("The current history token is: " + event.getValue());
}
}
The problem is that if you refresh the application, the history stack gets blown away. How do you preserve the history so that if the user refreshes the page, the back button is still useful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚使用 Firefox 和 Chrome 对我的应用程序进行了测试,页面刷新没有清除历史记录。 您使用哪种浏览器? 你的 HTML 中有吗
?
I have just tested it with Firefox and Chrome for my application and page refresh does not clear the history. Which browser do you use? Do you have the
in your HTML?
GWT 通过提供 History 对象来解决这个问题。 通过调用其静态方法
History.newItem("your token")
,您将能够将令牌传递到查询字符串中。不过,您需要注意,只要 gwt 应用程序中发生历史记录更改,就会触发
onValueChange(ValueChangeEvent event){}
事件,并且您可以在该方法中调用相应的页面。 以下是我用来解决此问题的步骤列表。向需要调用新页面的对象添加点击监听器。 在处理事件时,向
history 添加一个令牌。(History.newItem("new_token")
。在实现 EntryPoint 的类中实现
ValueChangeHandler
。将
onValueChangeHandler(this)
侦听器添加到实现 EntryPoint 的类中。实现 EntryPoint 的类的onModuleLoad()
方法(重要的是添加到这个方法中)(非常明显哈!)最后实现
onValueChange(ValueChangeEvent event)
{ //call a new page } 方法。就是这样
GWT has catered for this problem by providing the History object. By making a call to it's static method
History.newItem("your token")
, you will be able to pass a token into your query string.However you need to be aware that any time there is a history change in a gwt application, the
onValueChange(ValueChangeEvent event){}
event is fired, and in the method you can call the appropriate pages. Below is a list of steps which i use to solve this problem.Add a click listener to the object that needs too call a new page. In handling the event add a token to the
history.(History.newItem("new_token")
.Implement the
ValueChangeHandler
in the class that implements your EntryPoint.Add
onValueChangeHandler(this)
listener to the class that implements the EntryPoint. Ensure that the line is add in theonModuleLoad()
method (it is important it is added in this method) of the class that implements the EntryPoint(pretty obvious ha!)Finally implement
onValueChange(ValueChangeEvent event)
{ //call a new page } method.That's it