GWT RPC 范围问题

发布于 2024-12-09 09:39:02 字数 1167 浏览 0 评论 0原文

我在保存 RPC 结果时遇到问题。基本上调用是成功的,但是当我尝试将其保存在变量中时,它似乎超出了范围。

private String userName = "default";

authService.retrieveUserData(new AsyncCallback<UserData>() {
        @Override
        public void onSuccess(UserData result) {
            userName = result.getUserName();
            shell.setError("The userName is " + userName + "!");
        }
        @Override
        public void onFailure(Throwable caught) {
            //I've checked and it is not failing.
        }
    });

shell.getLoginWidget().setUserName(userName);

shell 是一个添加到我的 RootLayoutPanel 中的 Composite。它包含一个 DivElement,其 InnerText 由 shell.setError(String) 设置。 shell 还有一个带有 SpanElement 的 LoginWidget,其 InnerText 通过 shell.getLoginWidget().setUserName(String) 设置。 userName 显然是一个初始化为“default”的字符串。

问题是 shell.setError("The userName is " + userName + "!"); (在 AsyncCallback 内部)将 InnerText 设置为当前登录的用户。但是 shell.getLoginWidget().setUserName(userName); (AsyncCallback 之外)将 InnerText 设置为“默认”。

我尝试了很多不同的事情,它们似乎都有相同的范围问题。真正让我困惑的是这个示例显示的内容几乎完全相同保存 RPC 结果的技术。

I'm having a problem saving the result of a RPC. Basically the call is successful but when I try to save it in a variable it seems to go out of scope.

private String userName = "default";

authService.retrieveUserData(new AsyncCallback<UserData>() {
        @Override
        public void onSuccess(UserData result) {
            userName = result.getUserName();
            shell.setError("The userName is " + userName + "!");
        }
        @Override
        public void onFailure(Throwable caught) {
            //I've checked and it is not failing.
        }
    });

shell.getLoginWidget().setUserName(userName);

shell is a Composite that is added to my RootLayoutPanel. It contains a DivElement of which the InnerText is set by shell.setError(String). shell also has a LoginWidget with a SpanElement of which the InnerText is set by shell.getLoginWidget().setUserName(String). userName is obviously a String that is initialized to "default".

The problem is that the shell.setError("The userName is " + userName + "!"); (inside the AsyncCallback) sets the InnerText to whoever is currently logged in. BUT shell.getLoginWidget().setUserName(userName); (outside the AsyncCallback) sets the InnerText to "default".

I have tried so many different things and they all seem to have the same scope problem. What really confuses me is that this example shows almost exactly the same technique for saving the result of the RPC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷荒 2024-12-16 09:39:02

GWT RPC 方法是异步 - 它们的返回值无法立即可用。您发布的代码的控制流程如下:

  1. userName 设置为“default”。
  2. authService 询问 UserData。这会为“UserData.xml”触发异步 XMLHttpRequest
  3. 将登录小部件中的 userName 设置为 userName 的当前值,即“默认”。
  4. 一段时间后,步骤 2 中发起的异步请求将返回,此时 userName 变量将设置为返回值。

正确的做法是在 AsyncCallbackonSuccess 方法中设置用户名:

public void onSuccess(UserData result) {
  userName = result.getUserName();
  shell.getLoginWidget().setUserName(userName);
}

GWT RPC methods are asynchronous - their return values are not available immediately. The flow of control for the code you posted is as follows:

  1. Set userName to "default".
  2. Ask the authService for the UserData. This fires off an asynchronous XMLHttpRequest for the `UserData.
  3. Set the userName in the login widget to the current value of userName, which is "default".
  4. Some time later the asynchronous request initiated in step 2 will return at which point the userName variable will be set to the returned value.

The correct thing to do is to set the user name in the onSuccess method of the AsyncCallback:

public void onSuccess(UserData result) {
  userName = result.getUserName();
  shell.getLoginWidget().setUserName(userName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文