![](/img/trans.png)
[英]GWT History - Does state create History or History results in state?
[英]History GWT demo - does not work
我已經在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方法中,param historyToken為空字符串,不應該是堆棧(first_page)中的值嗎?
如果有人發現問題或向我解釋我在哪里犯錯,我將不勝感激。
謝謝
在首頁加載時,您需要onHistoryChanged(INIT_STATE)
調用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(..)
,然后根據歷史記錄從該面板切換面板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.