1

現在のプロジェクトで履歴を実装するつもりなので、gwt で履歴をしばらくプレイしました。そのため、動作を確認して練習するためだけに小さなデモ プロジェクトを作成しました。そのため、何らかの理由で機能しません。コードは次のとおりです。前後に移動するのは非常に簡単です。

ここに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 は空の文字列です。

誰かが問題を見つけたり、私がどこで間違いを犯したかを説明してくれたりすると、私は感謝します.

ありがとう

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

一番上のパネル (EntryPoint または ContentPanel) にのみ履歴リスナーを登録History.addHistoryListener(..)し、そこからの履歴に基づいてパネルを切り替えることをお勧めします。

于 2011-06-27T13:10:03.463 に答える