2

这是我的历史值更改事件处理程序:

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

    if (token != null) {

      if (token.equals("!list")) {
          GWT.runAsync(new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
            }

            public void onSuccess() {
                presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
                presenter.go(container);
            }
        });
      }
      else if (token.equals("!add")) {
          GWT.runAsync(new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
            }

            public void onSuccess() {
                presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
                presenter.go(container);
            }
        });
      }
      else if (token.equals("!edit")) {
          GWT.runAsync(new RunAsyncCallback() {

                public void onFailure(Throwable caught) {
                }

                public void onSuccess() {
                    presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
                    presenter.go(container);
                }
          });
      }

    }

如您所见,转到 www.domain.com/#edit 会加载编辑视图。但是,我将如何在片段中指定一个参数,例如一个 id,并将其传递给 Edit Contacts Presenter?

www.domain.com/#edit/1

4

2 回答 2

1

首先,您的示例看起来很糟糕,因为添加和编辑案例在成功上做完全相同的事情。但我敢肯定你已经知道了 ;-)

我从 1.5 开始就没有使用过 GWT,但是从内存中我们通过字符串匹配来做到这一点,例如:

if (token.startsWith("edit")) {
  String userID = token.substring("edit".length() + 1);
  //...
}

我希望在较新版本的 GWT 中有帮助,因为将对象模型的位序列化和反序列化为 URL 安全令牌以支持历史记录是更痛苦的 GWT 主义之一。

于 2010-07-15T13:00:21.053 回答
1

您通过的令牌event.getValue()只是一个字符串 - 因此您可以使用它token.split("/")来获取所有片段,然后根据例如第一个片段继续(如果我们得到“编辑”,那么我们应该期待下一个数字,等等) .

于 2010-07-15T15:00:24.337 回答