【问题标题】:Why page cannot get bookmarked with History mechanism in GWT/MVP4G.?为什么页面不能用 GWT/MVP4G 中的历史机制添加书签。?
【发布时间】:2016-08-06 23:57:17
【问题描述】:

我正在尝试使用 GWT 应用程序实现历史记录机制,但页面书签存在问题,即在我的情况下,我创建了 3 个页面,其中一个页面被另一个页面调用。现在,问题是如果 page3 被添加了书签,那么在调用该书签时它应该打开 page3 而不是现在它打开主页。 为什么会这样。?可能是什么问题?

我已将 HistoryConverter 实现为,

@History(type=HistoryConverterType.SIMPLE)
public class MyHistoryConverter implements HistoryConverter<HistoryManagerEventBus> {

public MyHistoryConverter() {

}

@Override
public void convertFromToken(String historyName, String param,HistoryManagerEventBus eventBus) {
    eventBus.dispatch(historyName);     
}

public String convertToToken(String eventType){ 
   return eventType;
}

public String convertToToken(String eventType,HistoryPageTwoView view){    
    return view.getClass().getName();
}

public String convertToToken(String eventType,HistoryPageThreeView view){    
    return view.getClass().getName();
}

@Override
public boolean isCrawlable() {
    return false;
}

}

和 eventBus 一样,

@Events(startPresenter = HistoryPageOnePresenter.class,historyOnStart=true)
public interface HistoryManagerEventBus extends EventBusWithLookup {

/**
 * Start event will be fired internally
 */
@Start
@Event(handlers = HistoryPageOnePresenter.class,historyConverter=MyHistoryConverter.class)
void start();

@InitHistory
@Event(handlers = HistoryPageOnePresenter.class)
void init();

@Event(handlers = HistoryPageTwoPresenter.class,historyConverter=MyHistoryConverter.class)
void getHistoryPageTwo();

@Event(handlers=HistoryPageThreePresenter.class,historyConverter=MyHistoryConverter.class)
void getHistoryPageThree();

@Event(handlers=HistoryPageOnePresenter.class,historyConverter=MyHistoryConverter.class)
void getHistoryPageOne();

@Event(handlers=HistoryPageOnePresenter.class)
void setHistoryPageTwo(HistoryPageTwoView view);

@Event(handlers=HistoryPageOnePresenter.class)
void setHistoryPageThree(HistoryPageThreeView view);
}

【问题讨论】:

    标签: java gwt browser-history gwt-mvp mvp4g


    【解决方案1】:

    假设:

        @Event(handlers = HistoryPageTwoPresenter.class,historyConverter=MyHistoryConverter.class)
        void getHistoryPageTwo();
    
        @Event(handlers=HistoryPageThreePresenter.class,historyConverter=MyHistoryConverter.class)
        void getHistoryPageThree();
    
        @Event(handlers=HistoryPageOnePresenter.class,historyConverter=MyHistoryConverter.class)
        void getHistoryPageOne();
    

    是您的导航事件,不需要在 MyHistoryConverter 类中定义以下方法:

    public String convertToToken(String eventType,HistoryPageTwoView view){    
        return view.getClass().getName();
    }
    
    public String convertToToken(String eventType,HistoryPageThreeView view){    
        return view.getClass().getName();
    }
    

    因为它们不会被调用来创建历史令牌。

    如果您的历史转换器工作正常,您应该会在您的 URL 中看到类似的内容:

    [myURL]#getHistoryPageOne

    [myURL]#getHistoryPageTwo

    [myURL]#getHistoryPageThree

    如果你输入:

    [myURL]#getHistoryPageThree

    要启动您的应用程序,令牌将在 convertFromToken 方法中处理。 您可以将 @Debug-annotation 添加到您的 eventBus 以验证书签事件是否在您的应用程序启动时触发。

    所以一切看起来都不错,除了 Start-event 不应该有 historyConverter-attribute 的事实。

    【讨论】:

    • EI Hoss:即使我没有添加@Debug-annotation,在从启动事件中删除historyConverter-attribute后它仍然有效,但是在这种情况下,如果我转到第3页并按两次后退按钮应该转到没有发生的第 1 页。如果我们将 historyConverter-attribute 添加到开始事件,它会出现在 page1 但书签在这种情况下不起作用。你能解释一下这个场景吗?
    • @Debug 注释显示有关触发和消费事件的信息。不多。
    • 按两次后退按钮是什么意思。您应该会看到上面发布的 URL。如果您看到除此之外的其他 URL,您还可以将历史转换器用于其他事件。你呢?
    • 我的意思是说,当我在 page3 上时,Url 是 [demoHistoryManager.html#getHistoryPageThree?getHistoryPageThree]。现在,如果我按下浏览器后退按钮,URL 显示为 [demoHistoryManager.html#getHistoryPageTwo?getHistoryPageTwo] 即它来到第二页,但如果我再次按下浏览器后退按钮,它应该转到第一页,并且 url 应该是 [demoHistoryManager.html# getHistoryPageOne?getHistoryPageOne] 并非如此。在我的情况下,它仍然在第二页,网址是 [demoHistoryManager.html]
    • 历史只会回到您之前访问过的页面。
    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2019-09-05
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多