Java 历史GWT演示-不工作
我在gwt中玩了一段时间的历史,因为我打算在我当前的项目中实现它,所以我创建了一个小的演示项目,看看它是如何工作的,并进行一些实践。 因此,出于某些原因,它不起作用。 下面是代码-非常简单的前后转换 这是iframeJava 历史GWT演示-不工作,java,gwt,history,gwt-history,Java,Gwt,History,Gwt History,我在gwt中玩了一段时间的历史,因为我打算在我当前的项目中实现它,所以我创建了一个小的演示项目,看看它是如何工作的,并进行一些实践。 因此,出于某些原因,它不起作用。 下面是代码-非常简单的前后转换 这是iframe <iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe> 3个单件表单,加载表单的内容和2个表单 public class ContentPanel extend
<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
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());
}
}
最后但并非最不重要的是:))
问题是,我单击FirstPanel中的按钮,然后加载SecandPanel,然后在浏览器中按“后退”不做任何事情。在onHistoryChanged方法中,参数historyToken是空字符串,不应该是堆栈中的值(第一页)
如果有人发现问题或向我解释我的错误,我将不胜感激
感谢在您手动调用的
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)中,并根据那里的历史切换面板。你的意思是在最上面的面板上只有一个监听器,对吗?我知道这是一个旧的答案,但addHistoryListener现在已被弃用。现在应该使用它:History.addValueChangeHandler(新的ValueChangeHandler(){public void onValueChange(ValueChangeEvent事件){
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());
}
}
public FirstPanel() {
History.addHistoryListener(this);
String token = History.getToken();
if (token.length() == 0) {
History.newItem(INIT_STATE);
} else {
History.fireCurrentHistoryState();
}
.. rest of code