1

我在 gwt 中使用 History 玩了一段时间,因为我打算在我当前的项目中实现它,所以我创建了一个小型演示项目,只是为了看看它是如何工作的并做一些练习。因此,由于某些原因,它不起作用。这是代码 - 非常简单的来回前进。

这是 iframe

<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>

然后是入口点

public class EntryPoint implements com.google.gwt.core.client.EntryPoint {

private FirstPanel panel;

public void onModuleLoad() {

  ContentPanel panel = ContentPanel.getInstance();
  panel.setContent(FirstPanel.getInstance());
  RootPanel.get().add(panel);

}
} 

和 3 个单例表单,正在加载表单的内容和 2 个表单。

public class ContentPanel extends Composite
{
private static ContentPanel instance;
private static VerticalPanel panel = new VerticalPanel();
private ContentPanel()
{
  initWidget(panel);
}

public void setContent(Widget widget)
{
  panel.clear();
  panel.add(widget);
}
public static ContentPanel getInstance()
{
   if(instance == null)
     return instance = new ContentPanel();
   else
     return instance;
}
}

和 ..

public class FirstPanel extends  Composite implements HistoryListener {

private static FirstPanel instance;
private VerticalPanel panel;

public FirstPanel() {

  History.addHistoryListener(this);

  panel = new VerticalPanel();
  panel.setStyleName("panelstyle");

  Button button2 = new Button("Next page");
  button2.addClickHandler(new ClickHandler()
  {
    public void onClick(ClickEvent event)
    {
      History.newItem("second_page");
    }
  });
  panel.add(button2);
  initWidget(panel);
}

public static FirstPanel getInstance()
{
  if(instance == null)
    return instance = new FirstPanel();
  else
    return instance;
}

public Widget getWidget() {
  return panel;
}

public void onHistoryChanged(String historyToken) {
 if(historyToken.equalsIgnoreCase("second_page"))
   ContentPanel.getInstance().setContent(SecondPanel.getInstance());
}

}

最后但并非最不重要的:))

    public class SecondPanel extends Composite  implements HistoryListener
    {
    private static SecondPanel instance;
    public SecondPanel()
    {
      History.addHistoryListener(this);
      VerticalPanel verticalPanel = new  VerticalPanel();
      TextBox firstnameBox = new TextBox();
      TextBox lastnameBox = new TextBox();
      Button submitButton = new Button("Click");

      verticalPanel.add(firstnameBox);
      verticalPanel.add(lastnameBox);
      verticalPanel.add(submitButton);

      submitButton.addClickHandler(new ClickHandler()
      {
        public void onClick(ClickEvent event)
        {
          History.newItem("first_panel");
          alert("You are in second panel");
        }
      });
      initWidget(verticalPanel);
    }

    public static SecondPanel getInstance()
{
    if(instance == null)
      return instance = new SecondPanel();
    else
      return instance;
}
    public void onHistoryChanged(String historyToken)
    {
      if(historyToken.equalsIgnoreCase("first_panel"))
        ContentPanel.getInstance().setContent(FirstPanel.getInstance());
    }
    }

问题是我单击 FirstPanel 中的按钮并加载 SecandPanel 然后在浏览器中按“返回”没有做任何事情。在 onHistoryChanged 方法中,参数 historyToken 是空字符串,不应该是堆栈中的值(first_page)?

如果有人发现问题或向我解释我在哪里出错,我将不胜感激。

谢谢

4

1 回答 1

3

在第一页加载时,您手动调用onHistoryChanged(INIT_STATE)。这不会改变历史。替换为:

public FirstPanel() {

    History.addHistoryListener(this);
    String token = History.getToken();
    if (token.length() == 0) {
        History.newItem(INIT_STATE);
    } else {
        History.fireCurrentHistoryState();
    }

    .. rest of code

更好的做法是History.addHistoryListener(..)仅在最顶部的面板(EntryPoint 或 ContentPanel)中注册 History 侦听器,并从那里根据历史记录切换面板。

于 2011-06-27T13:10:03.463 回答