如何重建 GWT 历史堆栈?

发布于 2024-07-25 14:38:21 字数 1285 浏览 7 评论 0原文

我正在使用一个更大的应用程序,但是 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 技术交流群。

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

发布评论

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

评论(2

橙幽之幻 2024-08-01 14:38:21

我刚刚使用 Firefox 和 Chrome 对我的应用程序进行了测试,页面刷新没有清除历史记录。 您使用哪种浏览器? 你的 HTML 中有吗

<iframe src="javascript:''" id='__gwt_historyFrame' style='position:absolute;width:0;height:0;border:0'></iframe>

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

<iframe src="javascript:''" id='__gwt_historyFrame' style='position:absolute;width:0;height:0;border:0'></iframe>

in your HTML?

甜点 2024-08-01 14:38:21

GWT 通过提供 History 对象来解决这个问题。 通过调用其静态方法 History.newItem("your token"),您将能够将令牌传递到查询字符串中。

不过,您需要注意,只要 gwt 应用程序中发生历史记录更改,就会触发 onValueChange(ValueChangeEvent event){} 事件,并且您可以在该方法中调用相应的页面。 以下是我用来解决此问题的步骤列表。

  1. 向需要调用新页面的对象添加点击监听器。 在处理事件时,向 history 添加一个令牌。(History.newItem("new_token")

  2. 在实现 EntryPoint 的类中实现 ValueChangeHandler

  3. onValueChangeHandler(this) 侦听器添加到实现 EntryPoint 的类中。实现 EntryPoint 的类的 onModuleLoad() 方法(重要的是添加到这个方法中)(非常明显哈!)

  4. 最后实现 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.

  1. 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").

  2. Implement the ValueChangeHandler in the class that implements your EntryPoint.

  3. Add onValueChangeHandler(this) listener to the class that implements the EntryPoint. Ensure that the line is add in the onModuleLoad() method (it is important it is added in this method) of the class that implements the EntryPoint(pretty obvious ha!)

  4. Finally implement onValueChange(ValueChangeEvent event){ //call a new page } method.

That's it

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