2011-06-27 56 views
1

我已經玩了一段時間了gwt中的歷史,因爲我打算在我當前的項目中實現它,所以我創建了一個小型演示項目,以便了解它是如何工作的並執行一些練習。 所以,由於某些原因,它不起作用。 這是代碼 - 非常簡單的前後。歷史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是空字符串,不應該是棧中的值(first_page)?

如果有人發現問題或解釋我在哪裏犯錯,我將不勝感激。

感謝

回答

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 

更好的做法是隻在基於歷史從那裏最頂部的面板(入口點或的ContentPanel)和開關面板註冊歷史聽衆History.addHistoryListener(..)

+0

謝謝彼得,你是對的,它的工作原理! – adgfs

+0

你的意思是在頂層面板上只有一個監聽器,對吧? – adgfs

+0

是的,它會使代碼更具可讀性。 –