【问题标题】:GWT - History.getToken() is always returning blank valueGWT - History.getToken() 总是返回空白值
【发布时间】:2014-06-24 06:26:59
【问题描述】:

我试图在我的 GWT 应用程序中获取上次访问的页面。如果有历史记录,我想跳过特定页面。为了获得上次访问的页面,我尝试History.getToken() 来获取文档中描述的当前令牌。但它总是返回空白令牌。

请帮忙。我是 GWT 的初级开发人员。

【问题讨论】:

  • 先告诉我你是如何设置历史令牌的?

标签: java gwt browser-history


【解决方案1】:

请查看Coding Basics History - GWT Project。


例如,一个名为 page1 的历史令牌将被添加到 URL,如下所示:

http://www.example.com/com.example.gwt.HistoryExample/HistoryExample.html#page1

当应用程序想要将占位符推送到浏览器的历史堆栈时,它只需调用History.newItem(token)。

当用户使用后退按钮时,将对任何添加为带有History.addValueChangeHandler() 的处理程序的对象进行调用。

由应用程序根据新令牌的值恢复状态。


请在您的申请中验证以下几点。

要使用 GWT 历史支持,您必须首先将 iframe 嵌入到您的主机 HTML 页面中。

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

然后,在您的 GWT 应用程序中,执行以下步骤:

  • 当您想要启用历史事件时,将历史令牌添加到历史堆栈。
  • 创建一个实现 ValueChangeHandler 接口的对象,解析新令牌(可通过调用 ValueChangeEvent.getValue() 获得)并更改应用程序状态以匹配。

问题: GWT - History.getToken() 总是返回空白值?

请再次检查 URL 并确认您已将新的历史令牌添加到历史堆栈,最重要的是在您的主机页面中包含历史框架。

查看源代码以了解为什么会得到空白值?

// History.class
public class History {

    private static HistoryImpl impl;

    static {
      impl = GWT.create(HistoryImpl.class);
      if (!impl.init()) {
        // Set impl to null as a flag to no-op future calls.
        impl = null;

        // Tell the user.
        GWT.log("Unable to initialize the history subsystem; did you "
            + "include the history frame in your host page? Try "
            + "<iframe src=\"javascript:''\" id='__gwt_historyFrame' "
            + "style='position:absolute;width:0;height:0;border:0'>"
            + "</iframe>");
      }
    }

    public static String getToken() {
        // impl is null if you have not included the history frame in your host page
        // if impl is null then it return blank value
        return impl != null ? HistoryImpl.getToken() : "";
    }
}

在History Management in GWT 上找到示例代码。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 2021-11-20
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2017-11-28
    • 2013-11-17
    相关资源
    最近更新 更多